【问题标题】:UITableViewCell gets reselected when scrolled back on screenUITableViewCell 在屏幕上回滚时被重新选择
【发布时间】:2011-06-17 03:28:32
【问题描述】:

在我正在开发的 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 的预期默认行为吗?或者可能是我的实施中的错误?在这种情况下取​​消选择此单元格的最佳方法是什么?

【问题讨论】:

    标签: cocoa-touch ios uitableview uiscrollview


    【解决方案1】:

    我要求自己解决这个问题:

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

    【讨论】:

    • 您是否仍然能够重现动画,它会在短时间内选择它,然后立即取消选择它?
    • @cr54 是的,你仍然可以得到这样的动画效果: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES] ; }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多