【问题标题】:How to dynamically add rows to a specific UITableView section?如何动态地将行添加到特定的 UITableView 部分?
【发布时间】:2014-05-23 04:41:44
【问题描述】:

我是一个新的 IOS 程序员,我遇到了一个问题。

我有一个包含 2 个部分的 UITableView,一个是静态的,另一个是动态的。

在特定操作中,我需要在运行时为第二部分添加新行..

我知道如何管理 UITableView ,但不知道特定部分

你能帮帮我吗?

向大家致以最诚挚的问候

【问题讨论】:

标签: ios objective-c uitableview


【解决方案1】:

你可以使用UITableViewinsertRowsAtIndexPaths:方法

//Update data source with the object that you need to add
[tableDataSource addObject:newObject];

NSInteger row = //specify a row where you need to add new row
NSInteger section = //specify the section where the new row to be added, 
//section = 1 here since you need to add row at second section

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];

【讨论】:

  • 它不起作用,我可以添加一个空白行进行测试?还是我的数据源需要为新行做好准备?我有两个部分,第一个是 0 或 1?
  • 您需要在插入新行之前设置好数据源(为了测试,您可以在特定索引处添加一个虚拟对象并检查)。如果你有两个部分,第一个为 0,第二个为 1。
  • 我没有为我的部分使用数据源,正如我所说,第一个的想法是静态的,第二个是动态的,我尝试为动态的,但是当我测试它时,这两个部分都充满了数据源......我怎样才能将数据保留在第一个?
  • 使用来自UITableView 委托方法的indexPath.sectionsection。例如:` In - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath` 检查if (indexPath.section == 0) 第一部分和indexPath.section == 1第二部分
  • 我明白了这个想法,并对其进行了测试,完美,但我还有一个疑问,我怎样才能将静态单元格保留在第一部分?我认为它与“重用”有关,我使用 .storyboard 插入的静态...我必须做什么,才能返回相同的单元格?
【解决方案2】:

你可以使用insertRowAtIndexPath一样的方法

 // Add the items into your datasource object first. Other wise you will end up with error
 // Manage the number of items in section. Then do

 NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:0 inSection:1];
 NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:1 inSection:1];
[self.tableView insertRowsAtIndexPaths:@[indexPath1,indexPath2] withRowAnimation:UITableViewRowAnimationTop];

这将在 section1 中插入两行。请记住,在执行此操作之前,您已经管理了数据源对象。

【讨论】:

    【解决方案3】:

    Swift 3 版本

    // Adding new item to your data source
    dataSource.append(item)
    // Appending new item to table view
    yourTableView.beginUpdates()
    // Creating indexpath for the new item
    let indexPath = IndexPath(row: dataSource.count - 1, section: yourSection)
    // Inserting new row, automatic will let iOS to use appropriate animation 
    yourTableView.insertRows(at: [indexPath], with: .automatic)
    yourTableView.endUpdates()
    

    【讨论】:

      【解决方案4】:

      您可以使用 scrolltoinfinite 方法执行此操作,但在此之前您必须导入 3rd 方 svpulltorefresh

       [self.tableViewMixedFeed addInfiniteScrollingWithActionHandler:^{
      
              CGFloat height = self.tableViewMixedFeed.frame.size.height;
      
              CGFloat contentYoffset = self.tableViewMixedFeed.contentOffset.y;
      
              CGFloat distanceFromBottom = self.tableViewMixedFeed.contentSize.height - contentYoffset;
      if(distanceFromBottom<contentYoffSet)
      {
       [weak insertRowAtBottom];
      }
       }];
      
      -(void)insertRowAtBottom
      {
       [self.tableViewMixedFeed beginUpdates];
          [self.tableViewMixedFeed insertRowsAtIndexPaths:indexPaths1 withRowAnimation:UITableViewRowAnimationTop];
      
           [self.tableViewMixedFeed endUpdates];
          [self.tableViewMixedFeed.infiniteScrollingView stopAnimating];
      }
      

      这里的 indexpaths1 是你想要插入到表格中的下一个单元格的索引路径数组。 尝试使用循环获取下一组数组并将它们存储到 indexpaths1 中。

      【讨论】:

      • CGFloat heigh = self.tableViewMixedFeed.frame.size.height 未使用
      【解决方案5】:

      [self.tableView insertRowsAtIndexPaths:@[indexPath1,indexPath2] withRowAnimation:UITableViewRowAnimationTop];

      使用这个方法..

      【讨论】:

      • 嗨 asdafd,这看起来像是对 Anil Varghese 答案的确认,请不要将其添加为答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 2019-03-18
      相关资源
      最近更新 更多