xcode 点击第一行时在 UITableView 中插入新行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15950069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:11:08  来源:igfitidea点击:

Insert new row in UITableView when tapping first row

iphoneobjective-cxcodeuitableview

提问by Korg

I'm new to iOS development. Therefore it would be nice if your answer could be as detailed as possible. My problem is the following: I want the cell in my second section to always say "add new row". If you click this cell a new row should be added on top. My current code is

我是 iOS 开发的新手。因此,如果您的回答尽可能详细,那就太好了。我的问题如下:我希望第二部分中的单元格总是说“添加新行”。如果您单击此单元格,则应在顶部添加一个新行。我目前的代码是

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

// Return the number of sections.
return 2;
 }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

// Return the number of rows in the section.
if (section == 0) {
    return 1;
}
else {

    return [entries count];
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath        *)indexPath
{
static NSString *simpleTableIdentifier = @"Cell";
static NSString *todayTableIdentifier = @"Notification";
UITableViewCell *cell = [tableView       dequeueReusableCellWithIdentifier:simpleTableIdentifier];
UITableViewCell *todaycell = [tableView dequeueReusableCellWithIdentifier:todayTableIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];


}
NSString *entry;
if (indexPath.section == 0) {
    entry = [notifications objectAtIndex:indexPath.row];
    cell = todaycell;
} else {
    entry = [entries objectAtIndex:indexPath.row];
}
cell.textLabel.text = entry;

return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

int selectedRow = indexPath.row;
NSLog(@"row pressed %d", selectedRow);
NSArray *paths = [NSArray arrayWithObject:
                  [NSIndexPath indexPathForRow:2 inSection:2]];
if (selectedRow == 0)
{   [entries addObject:@"a new Entry"];
    [[self tableView] insertRowsAtIndexPaths:paths
                            withRowAnimation:UITableViewRowAnimationTop];
}
else {
    [[self tableView] deleteRowsAtIndexPaths:paths
                            withRowAnimation:UITableViewRowAnimationTop];
  }
}

Unfortunately, my app keeps crashing.

不幸的是,我的应用程序不断崩溃。

2013-04-11 16:52:39.717 MyStore[12924:c07] *** Assertion failure in -[UITableView    _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:861
2013-04-11 16:52:39.718 MyStore[12924:c07] *** Terminating app due to uncaught exception '    NSInternalInconsistencyException', reason: 'attempt to delete row 2 from section 2, but there    are only 2 sections before the update'
*** First throw call stack:
(0x1faa012 0x13e7e7e 0x1fa9e78 0xb6d665 0xbb43c 0xca1f6 0xca271 0x5c6b 0xcb285 0xcb4ed    0xad55b3 0x1f69376 0x1f68e06 0x1f50a82 0x1f4ff44 0x1f4fe1b 0x1f047e3 0x1f04668 0x1bffc 0x21bd  0x20e5 0x1)
libc++abi.dylib: terminate called throwing an exception

回答by Rui Peres

That's normal, besides adding a new row (visually), you need to update your Data Source (the notifications array in this case). The UI is just the visualization of your Data Source. So:

这是正常的,除了添加新行(在视觉上)之外,您还需要更新您的数据源(在本例中为通知数组)。UI 只是数据源的可视化。所以:

[notifications addObject:@"a new Entry"];

[[self tableView] insertRowsAtIndexPaths:paths
                            withRowAnimation:UITableViewRowAnimationTop];

And when you delete:

当你删除时:

 [notifications removeObjectAtIndex:indexPath.row];

 [[self tableView] deleteRowsAtIndexPaths:paths
                            withRowAnimation:UITableViewRowAnimationTop];