(很高兴接受Swift或Objective-C的答案)
我的表视图有几个部分,当按下一个按钮时,我想在第0部分的末尾插入一行.再次按下该按钮,我想删除同一行.我几乎工作的代码看起来像这样:
// model is an array of mutable arrays, one for each section
- (void)pressedAddRemove:(id)sender {
self.adding = !self.adding; // this is a BOOL property
self.navigationItem.rightBarButtonItem.title = (self.adding)? @"Remove" : @"Add";
// if adding, add an object to the end of section 0
// tell the table view to insert at that index path
[self.tableView beginUpdates];
NSMutableArray *sectionArray = self.model[0];
if (self.adding) {
NSIndexPath *insertionPath = [NSIndexPath indexPathForRow:sectionArray.count inSection:0];
[sectionArray addObject:@{}];
[self.tableView insertRowsAtIndexPaths:@[insertionPath] withRowAnimation:UITableViewRowAnimationAutomatic];
// if removing, remove the …Run Code Online (Sandbox Code Playgroud) 我有一个从Web服务中提取的数据列表.我刷新数据,我想在当前数据上方的表视图中插入数据,但我想在tableview中保持当前的滚动位置.
现在我通过在当前部分上方插入一个部分来完成此操作,但它实际上是插入,向上滚动,然后我必须手动向下滚动.我尝试在此之前禁用滚动表,但这也不起作用.
这看起来很不稳定,看起来很黑.有什么更好的方法呢?
[tableView beginUpdates];
[tableView insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];
NSUInteger iContentOffset = 200; //height of inserted rows
[tableView setContentOffset:CGPointMake(0, iContentOffset)];
Run Code Online (Sandbox Code Playgroud)