如何动态地将行添加到特定的UITableView部分?
我是一名新的IOS程序员,我遇到了一个问题。
我有一个UITableView有2个部分,一个是静态的,另一个是动态的。
在特定的操作中,我需要在运行时为第二部分添加新行。
我知道如何管理UITableView,但不是特定的部分
请问你能帮帮我吗?
最好的问候你们
您可以使用UITableView
insertRowsAtIndexPaths:
方法
//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];
您可以使用与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插入两行。 请记住,在执行此操作之前,您已经管理了数据源对象。
你可以通过使用scrolltoinfinite方法做到这一点,但在此之前,你必须导入第三方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
这里indexpaths1是您想要在表中插入的下一个单元格的索引路径数组。 尝试使用循环来获取下一组数组并将它们存储到indexpaths1中。
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()
[self.tableView insertRowsAtIndexPaths:@ [indexPath1,indexPath2] withRowAnimation:UITableViewRowAnimationTop];
使用这种方法..