2009-05-22 65 views
45

在我的UITableView中,有时单元格会在触摸后保持选中状态。 因为它只是偶尔发生,所以我无法重现 问题。iPhone 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; 
} 
    } 
+3

也许你重写viewWillAppear:动画没有调用[super viewWillAppear:animated]? – Tieme 2012-03-13 10:21:08

回答

98

我不能告诉你为什么你遇到的问题,但这里有一些建议修复它:

据苹果HIG,选择不应该消失,直到从视图返回控制器刚刚推入堆栈。如果你的控制器只是一个UITableViewController,它应该在返回到视图时自动取消选择。如果不是,则添加

- (void) viewWillAppear:(BOOL)animated { 
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:animated]; 
    [super viewWillAppear:animated]; 
} 

视图控制器中的某处。

如果有,点击后,没有选择的时候去另一种观点认为,不,其实做任何事情任何行,他们不应该是可选择的,这样就可以覆盖在

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath 

并且在不应该选择该行的情况下返回nil

11

为了展开“Ed Marty”的回答,我选择在“viewWillDisappear”方法中添加取消选择,因为我认为它看起来更好。

除此之外,我正在使用UITableView与常规UIViewController(而不是UITableViewController),所以tableView变量不可用于我。为了克服这一点,我在我的视图控制器头文件中添加了的tableView属性(在我的XIB文件我实际连接的表视图属性)...

@property (nonatomic,retain) IBOutlet UITableView *tableView 

...并在视图控制器实现有线向上的属性和如下进行取消选定..

@synthesize tableView 

- (void) viewWillDisappear:(BOOL)animated { 
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:animated]; 
    [super viewWillDisappear:animated]; 
} 
+0

谢谢,IBOutlet信息帮助了我。 – 2011-11-17 21:34:34

+0

由于所选答案在导航控制器中不起作用,所以此解决方案效果最佳 – codingrhythm 2013-01-05 07:52:30

21

一个可能的原因是覆盖-viewWillAppear:animated:而不延伸UITableViewController控制器调用[super viewWillAppear]。在您的-viewWillAppear方法开始处添加[super viewWillAppear:animated]可能会纠正问题。

+0

这就是我要的,谢谢! – 2013-12-31 04:45:18

8

如果您正在使用一个UITableViewController你可以使用:

self.clearsSelectionOnViewWillAppear = YES; 
4

就我而言,我取消细胞就在didSelectRowAtIndexPath方法的末尾:

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

    // Do smth here. segue ..etc 

    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO]; 
} 
0

使用此方法在你UITableViewCell

(void)setSelected:(BOOL)selected animated:(BOOL)animated { 

// Just comment This line of code 

// [super setSelected:selected animated:animated]; 

} 

甚至你可以在didSelectRowAtIndexPath方法中尝试这段代码d

- (空)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath {

//做当行选择一些东西

[tableView deselectRowAtIndexPath:indexPath animated:YES]; 

}

0

解决这个问题大约需要两秒钟,一旦你了解了表的管理方式。

只要单元格在屏幕上滚动,就会失去格式。该表通过关闭其他单元并在用户回滚时重新创建它们来最大限度地减少其内存使用量。

甚至像重新加载表格数据这样的事情,真的只适用于当时屏幕上的单元格。要指出的是,cellForRowAtIndexPath可能会创建同一个单元五次,而用户无需点击按钮或离开视图控制器!所以设置它来重新加载任何格式。像这样添加到cellForRowAtIndexPath:

if arrayOf_SelectedCells.contains(indexPath) { 
     setup_Selected(cell) 
     //Where setup_Selected is a custom function to apply formatt 
    } else { 
     setup_Unselected(cell) 
    } 
相关问题