Ios 如何向特定UITableView节动态添加行?

Ios 如何向特定UITableView节动态添加行?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我是一名新的IOS程序员,我遇到了一个问题 我有一个UITableView,有两个部分,一个是静态的,另一个是动态的 在特定操作中,我需要在运行时为第二部分添加新行 我知道如何管理UITableView,但不知道如何管理特定部分 你能帮我吗 向大家问好您可以使用相同的方法insertrowatinexpathlike // Add the items into your datasource object first. Other wise you will end up with error

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

我有一个UITableView,有两个部分,一个是静态的,另一个是动态的

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

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

你能帮我吗


向大家问好

您可以使用相同的方法
insertrowatinexpath
like

 // 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];

这将在截面1中插入两行。请记住,在执行此操作之前,您必须管理数据源对象

您可以使用
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];

您可以使用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];
}
[self.tableViewMixedFeed addInfiniteScrollingWithActionHandler:^{
CGFloat height=self.tableViewMixedFeed.frame.size.height;
CGFloat contentYoffset=self.tableViewMixedFeed.contentOffset.y;
CGFloat DISTANCE FROMBOTTOM=self.tableViewMixedFeed.contentSize.height-contentYoffset;

if(与底部的距离[self.tableView insertRowsAtIndexPaths:@[indexPath1,indexPath2]带行动画:uitableview行动画顶部]


使用此方法。

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

它不起作用,我可以为测试添加一个空行?或者我的数据源需要为新行做好准备?我有两个部分,第一个是0或1?在插入新行之前,您需要将数据源设置为就绪(对于测试,您可以在特定索引处添加一个虚拟对象并进行检查)如果你有两个部分,第一个部分是0,第二个部分是1。我没有为我的部分使用数据源,正如我所说的,第一个部分的想法是静态的,第二个是动态的,我尝试为动态的部分实现一个数据源,但是当我测试它时,两个部分都被数据源填满了…如何我是否将数据保留在第一个方法中?使用
indexPath.section
UITableView
委托方法中的
section
。例如:`in-(UITableViewCell*)tableView:(UITableView*)tableView cellforrowatinexpath:(NSIndexPath*)indexath`检查
if(indexPath.section==0)
对于第一部分和
indexPath.section==1
对于第二部分,我得到了想法,并对其进行了测试,非常完美,但我还有一个疑问,我怎么能在第一部分保留静态单元格?我想这与“重用”有关,我使用.storyboard插入的静态单元格…我必须做什么才能返回相同的单元格?嗨,asdafd,这看起来像是对Anil Varghese答案的确认,请不要将其添加为答案。未使用Value CGFloat heigh=self.tableViewMixedFeed.frame.size.height