在我的 UITableView 中,有时单元格在触摸后仍保持选中状态。 因为这种情况只是偶尔发生,所以我无法重现 问题。
有什么提示吗?也许与tableView释放不当有关?
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
switch (row) {
case 0:
FruitViewController *fruitController = [FruitViewController alloc];
[fruitController retain];
[fruitController initWithNibName:@"FruitView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:fruitController animated:YES];
[fruitController release];
break;
case 1:
CerealsViewController *cerealsController = [CerealsViewController alloc];
[cerealsController retain];
[cerealsController initWithNibName:@"CerealsView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:cerealsController animated:YES];
[cerealsController release];
break;
default:
break;
}
}
最佳答案
我无法告诉您为什么会遇到此问题,但这里有一些解决该问题的建议:
根据 Apple HIG,选择内容在从刚刚压入堆栈的 View Controller 返回之前不应消失。如果您的 Controller 只是一个 UITableViewController,它应该在返回 View 时自动取消选择。如果没有,请添加
- (void) viewWillAppear:(BOOL)animated {
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:animated];
[super viewWillAppear:animated];
}
View Controller 中的某个位置。
如果有任何行在单击时不会转到另一个 View ,并且实际上在选择时不会执行任何操作,则它们不应该是可选择的,因此您可以在
中覆盖它- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
在不应选择该行的情况下返回nil
。
关于iPhone UITableView 单元格保持选中状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/897071/