簡體   English   中英

iOS以編程方式添加UITableView部分並重新加載

[英]iOS Add UITableView section programmatically and reload it

我想將Section添加到UITableView並將Rows添加到這個新的Section中,但是當我這樣做時,應用程序崩潰了。

如何不使用UITableView.ReloadData()將數據添加到UITableView並重新加載數據。

這是我使用它的方法。

 private void AddToSection(UITableView tableView, List<TableItems> messages, bool reload = false)
  {

     foreach (var item in messages)
     {

        if (!_section.Contains(item.Section))
        {
           _sectionMessage.Add(new List<string>());
           _section.Add(item.Section);
           if (reload)
           {                  
              var index = NSIndexSet.FromIndex(_section.Count - 1);
              tableView.InsertSections(index, UITableViewRowAnimation.None);                  
           }

        }
        int section = _section.Count - 1;
        _sectionMessage[section].Add(item.Message);

        if (reload)
        {
           int row = _sectionMessage.LastOrDefault().Count-1;
           var index = new NSIndexPath[] {NSIndexPath.FromRowSection(row, section) };
           tableView.InsertRows(index, UITableViewRowAnimation.None);             
        }

     }
  }

首先編輯:您還必須更新dataSource。 您不能僅在不更新dataSource的情況下將新的部分和單元格添加到tablView中。

///您是否嘗試了以下方法?

tableView.beginUpdates()
tableView.endUpdates()

通常,當您執行reloadData()並且未正確更新dataSource時,會遇到類似您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM