【发布时间】:2014-05-23 04:41:44
【问题描述】:
我是一个新的 IOS 程序员,我遇到了一个问题。
我有一个包含 2 个部分的 UITableView,一个是静态的,另一个是动态的。
在特定操作中,我需要在运行时为第二部分添加新行..
我知道如何管理 UITableView ,但不知道特定部分
你能帮帮我吗?
向大家致以最诚挚的问候
【问题讨论】:
标签: ios objective-c uitableview
我是一个新的 IOS 程序员,我遇到了一个问题。
我有一个包含 2 个部分的 UITableView,一个是静态的,另一个是动态的。
在特定操作中,我需要在运行时为第二部分添加新行..
我知道如何管理 UITableView ,但不知道特定部分
你能帮帮我吗?
向大家致以最诚挚的问候
【问题讨论】:
标签: ios objective-c 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];
【讨论】:
UITableView 委托方法的indexPath.section 或section。例如:` In - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath` 检查if (indexPath.section == 0) 第一部分和indexPath.section == 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 中插入两行。请记住,在执行此操作之前,您已经管理了数据源对象。
【讨论】:
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()
【讨论】:
您可以使用 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 中。
【讨论】:
[self.tableView insertRowsAtIndexPaths:@[indexPath1,indexPath2] withRowAnimation:UITableViewRowAnimationTop];
使用这个方法..
【讨论】: