2011-01-28 57 views
8

我在iOS iOS应用程序(iOS SDK 4.2)中实现UITableView时遇到了问题。如果我点击表格视图中的单元格,然后滚动视图以便单元格离开屏幕,当它返回到视图中时,最近选择的单元格已被重新选择。在屏幕上滚动时重新选择UITableViewCell

为了验证这一点,我创建了一个新的视图基于应用程序项目,在Interface Builder中拖出一个UITableView,并设置视图控制器的表视图的数据源和委托,并把下面的代码视图控制器:

- (NSInteger)numberOfSectionsInTableView:(UITableView *) tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger) section { 
    return 12; 
} 

- (UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath { 
    static NSString *cellID = @"aCellID"; 

    UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:cellID]; 

    if (!aCell) { 
     aCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease]; 
    } 

    aCell.textLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row +1]; 

    return aCell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *aCell = [tableView cellForRowAtIndexPath:indexPath]; 
    [aCell setSelected:NO animated:YES]; 
} 

当我运行这个(在模拟器或设备上)如果我点击任何单元格(例如单元格12),它将被选中和取消选择。在此之后,在表中没有细胞当前选择,如果我滚动视图,以便(在这种情况下12)最近选择的细胞离开屏幕,它会再次当它再次选择。

这是UITableView的预期默认行为?或者可能是我的实现中的错误?在这种情况下取​​消选择此单元格的最佳方法是什么?

回答

13

我花了问摸不着出自己:

我的问题是使用UITableViewCell方法setSelected:,而不是使用UITableView方法deselectRowAtIndexPath:

+0

谢谢!我遇到过同样的问题。 – Marky 2011-04-05 13:51:32