Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin iOS getting duplicate data for each UITableView section

I am using MvvmCross for my Xamarin iOS project. For my tableview source I have say, a,b,c,d,e,f,g,h records and section(header): 0 of suitable view has 3 records and section: 1 had 4 then

Expected result

Section-0 a b c Section-1 d e f g

I'm getting: Section-0 a b c Section-1 a b c d

The records are being duplicated. I checked the source but source has correct data.

ISSUE:

At the end of each section the source is restarting from the top again to fill the records

My source:

View:

base.DoBind();
            var source = new TableSource<string> (Table, ViewModel);
            Table.Source = source;
            Table.ReloadData();
            Table.AlwaysBounceVertical = false;
            var set = this.CreateBindingSet<View, ViewModel>();
            set.Bind(source).To(vm => vm.CardsList);
            set.Apply();

Cell:

protected override void DoBind()
        {
            var set = this.CreateBindingSet<Cell, ListViewModel>();
            set.Bind(LblNum).To(vm => vm.CardNumber);
            set.Bind(Balance).To(vm => vm.Balance);
            set.Apply();
        }

CellViewModel

public class ListViewModel : MvxViewModel
    {
        private string _CardNumber;
        public string Number
        {
            get { return _Number); }
            set { _CardNumber = value; }
        }

        private string _Balance;
        public string Balance
        {
            get {  _Balance; }
            set { _Balance = value; }
        }

Can anyone please advice how to resolve this

Update

    UITableView _tableView;

            public TableSource(UITableView tableView, object item) : base(tableView)
            {
                this.viewModel = item as ViewModel;
                this._tableView = tableView;

                tableView.RegisterNibForCellReuse(HeaderCell.Nib, HeaderCell.Key);      tableView.RegisterNibForCellReuse(UINib.FromName(Cell.Key, NSBundle.MainBundle), Cell.Key);
                tableView.RegisterNibForHeaderFooterViewReuse(UINib.FromName(HeaderCell.Key, NSBundle.MainBundle), HeaderCell.Key);
                tableView.ReloadData();

                var DataDic = new Dictionary<string, List<string>>
                {
                    { "section1", new List<string> {}},
                    { "section2", new List<string> {}},
                    { "section3", new List<string> {}}
                };

                //create the data
                var list = new List<TableModel<string>>();
                foreach (var section in DataDic)
                {
                    var sectionData = new TableModel<string>()
                    {
                        Title = section.Key
                    };
                    foreach (var row in section.Value)
                    {
                        sectionData.Add(row);
                    }

                    list.Add(sectionData);
                }
                TableItems = list;

            } 
public override nint NumberOfSections(UITableView tableView)
        {
            return TableItems.Count;
        }

        public override nint RowsInSection(UITableView tableview, nint section)
        {
            int result = 0;
            if (section == 0)
            {
                result = viewModel.NumOfGiftCards;
            }
            else if (section == 1)
            {
                result = viewModel.NumOfRewardsCerts;
            }

            return result;
        }

        public override nfloat GetHeightForHeader(UITableView tableView, nint section)
        {
            return section == 2 ? 0f : 74f;
        }

        public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
        {
            return indexPath.Section == 2 ? 95f : 62f;
        }

        public override IEnumerable ItemsSource
        {
            get
            {
                return base.ItemsSource;
            }
            set
            {
                base.ItemsSource = value;
                _tableView.ReloadData();
            }
        }

        public override UIView GetViewForHeader(UITableView tableView, nint section)
        {
            var header = tableView.DequeueReusableHeaderFooterView(HeaderCell.Key) as HeaderCell;
            return header;
        }

        protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
        {
            var cell = tableView.DequeueReusableCell(Cell.Key, indexPath) as Cell;

            return cell;
        }

        public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
        {
            viewModel.CardDetailsCommand.Execute(null);
        }
like image 462
TheDeveloper Avatar asked Feb 05 '26 01:02

TheDeveloper


1 Answers

I don't think the ItemSource you are binding the CardsList to is not matching up to the TableItems, viewModel.NumOfRewardsCerts or viewModel.NumOfGiftCards that you are using the table source.

Have you had a looked at the MvxExpandableItemSource (the sample is here) It can handle 2 dimensional ItemSources:

private IEnumerable<TItemSource> _itemsSource;
public new IEnumerable<TItemSource> ItemsSource
{
    get
    {
        return _itemsSource;
    }
    set
    {
        _itemsSource = value;
        _sectionExpandableController.ResetState();

        ReloadTableData();
    }
}

It deals with expanding and collapsing the from the headers, see below, but you could disable that functionality and force them to be expanded all the time: enter image description here

like image 81
Iain Smith Avatar answered Feb 06 '26 18:02

Iain Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!