Question

have an UITableView with index and when I scroll to bottom load more cell from server,the problem is that when I try to add more cells shows "assertion failed" error, this is how I was trying to add cells. Note: Table view source its modified and without adding more cell works fine

List<ICContactTableItem>items,itemsAdd;
Dictionary<string, List<ICContactTableItem>> indexedTableItems;//parameter in tableviewsource
string[] keys;//parameter in tableviewsource
UITableView tableContact;

public void addContacts(){
        InvokeOnMainThread (delegate {
            try{
                tableContact.BeginUpdates();
                int count=items.Count;

                List<NSIndexPath> tmpArray = new List<NSIndexPath>(); 

                foreach(ICContactTableItem item in itemsAdd){
                    if(item.firstName.Length>0){
                        if (indexedTableItems.ContainsKey (item.firstName[0].ToString ())) {
                            indexedTableItems [item.firstName [0].ToString ()].Add (item);
                            NSIndexPath tmpIndexPath = NSIndexPath.FromRowSection(
                                indexedTableItems[item.firstName [0].ToString ()].Count-1,
                                getIndex(item.firstName [0].ToString ()));
                            tmpArray.Add(tmpIndexPath); 
                        } else {
                            indexedTableItems.Add (item.firstName [0].ToString (), new List<ICContactTableItem> () { item });
                            tableContact.InsertSections(NSIndexSet.FromIndex(getIndex(item.firstName [0].ToString ()))
                                ,UITableViewRowAnimation.None);
                            NSIndexPath tmpIndexPath = NSIndexPath.FromRowSection(
                                indexedTableItems[item.firstName [0].ToString ()].Count-1,
                                getIndex(item.firstName [0].ToString ()));
                            tmpArray.Add(tmpIndexPath);

                        }
                    }else{
                        if (indexedTableItems.ContainsKey (" ")) {
                            indexedTableItems [" "].Add (item);
                            NSIndexPath tmpIndexPath = NSIndexPath.FromRowSection(
                                indexedTableItems[" "].Count,
                                getIndex(" "));
                            tmpArray.Add(tmpIndexPath); 
                        } else {
                            indexedTableItems.Add (" ", new List<ICContactTableItem> () { item });
                            indexedTableItems [" "].Add (item);
                            tableContact.InsertSections(NSIndexSet.FromIndex(getIndex(" "))
                                ,UITableViewRowAnimation.None);
                            NSIndexPath tmpIndexPath = NSIndexPath.FromRowSection(
                                indexedTableItems[" "].Count,
                                getIndex(" "));
                            tmpArray.Add(tmpIndexPath); 


                        }

                    }
                }
                keys=indexedTableItems.Keys.ToArray();
                items.AddRange(itemsAdd);

                tableContact.InsertRows(tmpArray.ToArray(),UITableViewRowAnimation.None);
                tableContact.EndUpdates();

                tableContact.ReloadData();
                tableContact.SetNeedsLayout();

            }catch(Exception e){
                Console.WriteLine(e.StackTrace);
            }
            ICLayoutMgr.Get().SetBusy(false);

        });
    }
int getIndex (string str)
    {
        string[] keysOrdered = indexedTableItems.Keys.ToArray();
        int res = 0;
        for (int i = 0; i < keysOrdered.Length; i++) {
            if (keysOrdered [i] == str) {
                res = i;
            }
        }
        return res;

    }
Was it helpful?

Solution

Before you make any calls to tell the table to add or remove any rows, you must update your data source with by adding or removing data. The table will check how many sections and rows there are before and after your add/remove rows. The number of sections and rows after the change must properly reflect how much data you add/remove with how many rows you add/remove.

And of course you must implement the NumberOfRowsInSection method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top