gpt4 book ai didi

ios - UITableView:删除行和添加新行后,新行不可选择

转载 作者:搜寻专家 更新时间:2023-10-30 20:15:59 30 4
gpt4 key购买 nike

我有一个相当普通的 UIViewController 和一个 UITableView,我以前一定做过数百次。唯一的区别是我的表在编辑模式下只能添加或删除行(不确定我的问题是否与此相关)。

该表由核心数据支持。我正在使用 Xcode 6.2,针对 iOS 8.2。问题发生在模拟器和设备上。

问题是每次我删除行然后添加新行时,新行都会按预期出现在表格中但它们不可选择

tableView:didSelectRowAtIndexPath:tableView:willSelectRowAtIndexPath: 不会被调用。其他行就好了。

这已经困扰我一天了,起初我认为这是一种奇怪的不一致,但一旦我确定了失败模式,它就是 100% 一致的。

有趣的是,关闭应用程序并重新启动后,插入的行是可选的,所以我很确定问题出在表的内部状态,而不是与数据源有关。

我已经尝试了所有我能想到的方法,包括自由地将 [self.tableView reloadData] 调用放在所有内容之后并显式地重新设置 self.tableView.allowsSelectionDuringEditing = YES.

部分代码贴在下面。 (我已经粘贴了所有我能想到的似乎与错误相关的方法。)

- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.allowsSelectionDuringEditing = YES;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSLog(@"pages count %d", [self pages].count);
return [self pages].count + (tableView.editing ? 1 : 0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row < [[self pages] count])
{
PageListTableViewCell *cell = (PageListTableViewCell *)[tableView dequeueReusableCellWithIdentifier:kDefaultCellReuseIdentifier forIndexPath:indexPath];
PageModel *page = [self pages][indexPath.row];
// Configure the cell...
cell.displayNameLabel.text = page.displayName;
//cell.editing = [page.displayName isEqualToString:@""];
return cell;
}
else if (tableView.editing)
{
AddTableViewCell *cell = (AddTableViewCell *)[tableView dequeueReusableCellWithIdentifier:kAddCellReuseIdentifier forIndexPath:indexPath];
cell.textLabel.hidden = YES;
return cell;
}
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row < [[self pages] count])
{
// ... normal selection behaviour
}
else
{
// add new row
[self addPageAtIndexPath:indexPath];
}
}

- (void)addPageAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView beginUpdates];
// add row in core data
NSString *pageID = [NSString stringWithFormat:@"page-%ld-%@", indexPath.row, [[NSUUID UUID] UUIDString]];
PageModel *page = [[PageModelController sharedInstance] addPageWithIdentifier:pageID displayName:@"untitled"];
NSLog(@"Page %ld added, ID %@", indexPath.row, pageID);

[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.tableView beginUpdates];
// Delete the row from core data
PageModel *page = [self pages][indexPath.row];
[[PageModelController sharedInstance] removePage:page];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
}
}

最佳答案

终于找到了——感谢@Rory McKinnel 让我仔细研究了我的自定义单元实现。它实际上什么也没做,因为我在调试过程中去掉了它的内容,但关键是它仍然覆盖 prepareForReuse 并且错过了对 super 的调用>。哎哟。

关于ios - UITableView:删除行和添加新行后,新行不可选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30213172/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com