如何动态地将行添加到特定的UITableView部分?

时间:2014-05-23 04:41:44

标签: ios objective-c uitableview

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

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

在具体操作中,我需要在运行时为第二部分添加新行。

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

你能帮我吗?

最诚挚的问候你们

5 个答案:

答案 0 :(得分:12)

您可以使用insertRowsAtIndexPaths:

UITableView方法
//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];

答案 1 :(得分:1)

您可以使用相同的方法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中插入两行。请记住,在执行此操作之前,您已经管理了数据源对象。

答案 2 :(得分:1)

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()

答案 3 :(得分:0)

你可以使用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<contentYoffSet)
{
 [weak insertRowAtBottom];
}
 }];

-(void)insertRowAtBottom
{
 [self.tableViewMixedFeed beginUpdates];
    [self.tableViewMixedFeed insertRowsAtIndexPaths:indexPaths1 withRowAnimation:UITableViewRowAnimationTop];

     [self.tableViewMixedFeed endUpdates];
    [self.tableViewMixedFeed.infiniteScrollingView stopAnimating];
}

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

答案 4 :(得分:-1)

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

使用这种方法..

相关问题