UITableView:删除行和添加新行后,新行不可选择
UITableView: after deleting rows and adding new ones, new rows are not selectable
我有一个相当正常的 UIViewController
和一个 UITableView
,我以前一定做过数百次。唯一的区别是我的 table 只能在编辑模式下添加或删除行(不确定我的问题是否与此有关)。
table 是核心数据支持。我正在使用 Xcode 6.2,目标是 iOS 8.2。问题出现在模拟器和设备上。
问题是每次我删除行,然后添加新行,新行按预期出现在table中但它们不是选择table.
tableView:didSelectRowAtIndexPath:
和 tableView:willSelectRowAtIndexPath:
不会被调用。其他行就好了。
这已经困扰我一天了,一开始我认为这是一种奇怪的不一致,但一旦我确定了失败模式,它就是 100% 一致的。
有趣的是,关闭应用程序并重新启动后,插入的行是 selectable,所以我很确定问题出在 table 的内部状态而不是任何事情要做与数据源。
我已经尝试了所有我能想到的方法,包括自由地将 [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
[=15 的调用=].哎哟
我有一个相当正常的 UIViewController
和一个 UITableView
,我以前一定做过数百次。唯一的区别是我的 table 只能在编辑模式下添加或删除行(不确定我的问题是否与此有关)。
table 是核心数据支持。我正在使用 Xcode 6.2,目标是 iOS 8.2。问题出现在模拟器和设备上。
问题是每次我删除行,然后添加新行,新行按预期出现在table中但它们不是选择table.
tableView:didSelectRowAtIndexPath:
和 tableView:willSelectRowAtIndexPath:
不会被调用。其他行就好了。
这已经困扰我一天了,一开始我认为这是一种奇怪的不一致,但一旦我确定了失败模式,它就是 100% 一致的。
有趣的是,关闭应用程序并重新启动后,插入的行是 selectable,所以我很确定问题出在 table 的内部状态而不是任何事情要做与数据源。
我已经尝试了所有我能想到的方法,包括自由地将 [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
[=15 的调用=].哎哟