Cocoa touch UITableViewCell在屏幕上回滚时被重新选择

Cocoa touch UITableViewCell在屏幕上回滚时被重新选择,cocoa-touch,ios,uitableview,uiscrollview,Cocoa Touch,Ios,Uitableview,Uiscrollview,在我正在开发的iOS应用程序(iOS SDK 4.2)中实现UITableView时,我遇到了一个问题。如果我在表格视图中点击一个单元格,然后滚动视图,使该单元格离开屏幕,当它返回视图时,最近选择的单元格已被重新选择 为了测试这一点,我创建了一个新的基于视图的应用程序项目,在Interface Builder中拖出一个UITableView,并将视图控制器设置为表视图的数据源和委托,并将以下代码放在视图控制器中: - (NSInteger)numberOfSectionsInTableView:

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

为了测试这一点,我创建了一个新的基于视图的应用程序项目,在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
的预期默认行为吗?或者可能是我的实现中的错误?在这种情况下,取消选择此单元格的最佳方法是什么?

我自己想了想:


我的问题是使用
UITableViewCell
方法
setSelected:
而不是使用
UITableView
方法
取消浏览索引路径:

是否仍能重现动画,动画会选择它一小段时间,然后立即取消选择?@cr54是,您仍然可以获得如下动画效果:-(void)tableView:(UITableView*)tableView DidSelectRowatineXpath:(NSIndexPath*)indexPath{[tableView DescroatineXpath:indexPath animated:YES];}