Xamarin.iOS动态地将行添加到索引的uitableview

时间:2014-03-19 20:51:39

标签: ios uitableview xamarin.ios xamarin

有一个带索引的UITableView,当我滚动到底部从服务器加载更多单元格时,问题是当我尝试添加更多单元格时显示"断言失败"错误,这就是我试图添加单元格的方式。 注意:表视图源修改后无需添加更多单元格工作正常

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;

    }

1 个答案:

答案 0 :(得分:0)

在进行任何调用以告知表格添加或删除任何行之前,您必须通过添加或删除数据来更新数据源。该表将检查添加/删除行之前和之后的部分和行数。更改后的部分和行数必须正确反映您添加/删除的数据量以及添加/删除的行数。

当然,您必须实施NumberOfRowsInSection方法。