2014-05-23 53 views
5

我是一个新的IOS程序员,我有一个问题。如何将行动态添加到特定的UITableView部分?

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

在我需要在运行时的第二部分中添加新行的具体行动..

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

你能帮助我吗?

问候大家

+0

https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/ManageInsertDeleteRow/ManageInsertDeleteRow。 html – iPatel

回答

11

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

它不起作用,我可以添加一个空行来测试?或者我的数据源需要为新行准备好?我有两个部分,第一个是0或1? – vmfesta

+0

您需要在插入新行之前设置数据源(为了测试,您可以在特定索引处添加一个虚拟对象并进行检查)。如果你有两部分,第一部分将是0,第二部分将是1. – Akhilrajtr

+0

我没有为我的部分使用数据源,正如我所说的,第一部分的想法是静态的,第二部分动态的,我试图为动态的实现数据源,但是当我测试它时,两个部分都充满了数据源......我如何将数据保存在第一个数据中? – vmfesta

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。记住在做这个之前你已经管理了你的数据源对象。

0

insertRowsAtIndexPaths:方法,你可以通过使用scrolltoinfinite方法做到这一点,但在此之前,u必须导入第三方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是indexpaths的阵列您想要插入表中的下一个单元格。 尝试使用循环获取下一组数组并将它们存储到indexpaths1中。

+0

值** CGFloat高度** = self.tableViewMixedFeed.frame.size。高度不使用 – Malder

-1

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

使用此方法..

+1

嗨asdafd,这看起来像Anil Varghese答案的确认,请不要将此添加为答案。 – bummi

0

斯威夫特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() 
相关问题