iPhone UITableView 单元格保持选中状态

标签 iphone cocoa-touch uitableview

在我的 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/

相关文章:

iphone viewForZoomingInScrollView 未调用

iphone - 强制 Xcode 4 将 XIB 编译为 NIB

iphone - iOS:从网址下载图片并保存在设备中

arrays - 使用数组中的值将 Section 添加到 tableView

iOS 10.0 UIRefreshControl 不显示指示器

iphone - 当我在 Cocoa Touch 中加载一个新 View 时,它会将所有内容向上移动大约 20 像素

iPhone iOS 在后台运行的应用程序会生成和处理通知吗?

ios - UINavigationController pushViewController 在中途暂停/卡住

iphone - 通话结束后重新打开应用程序

ios - UITableView 重用不同部分的内容