【问题标题】:UITableViewCell selection stays in all cells !UITableViewCell 选择停留在所有单元格中!
【发布时间】:2009-08-31 17:55:16
【问题描述】:

我仍在熟悉 iPhone SDK。

这就是我想要做的:

我有一个 UIScrollView,每个滚动视图都有一个 UITableView,我已经实现了一个自定义 UITableViewCell。

所需的功能是最初没有选择,然后用户选择行并滚动并在下一个滚动视图中进行另一个选择并继续。我希望保留选择,以便用户稍后更改选择。

但是在我的情况下发生的事情是 - 第一个和第二个 UITableViews 都很好,选定的行保持选中状态,但是在第三个 UITableView 中我看到一行“已经”被选中,它与第一个 UITableView 相同。我不想看到任何被选中。

我知道我在做一些愚蠢的事情,但不知道是什么。任何帮助将不胜感激。

谢谢, 艾米

这里是相关的数据源和委托方法。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"%s", __FUNCTION__); static NSString * ChoiceTableCellIdentifier = @"ChoiceTableCellIdentifier"; choiceTableCell = (ChoiceTableCell *)[tableView dequeueReusableCellWithIdentifier:ChoiceTableCellIdentifier]; if( choiceTableCell == nil ) { choiceTableCell = [[[ChoiceTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:ChoiceTableCellIdentifier] autorelease]; } return choiceTableCell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"%s", __FUNCTION__); int newRow = [indexPath row]; int oldRow = [lastIndexPath row]; if ( (newRow != oldRow) || (newRow == 0) ) { UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; UIImageView *indicatorN = (UIImageView *)[newCell.contentView viewWithTag:SELECTION_INDICATOR_TAG_1]; indicatorN.image = [UIImage imageNamed:@"selected.png"]; newCell.backgroundView.backgroundColor = [UIColor clearColor]; UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath]; UIImageView *indicatorO = (UIImageView *)[oldCell.contentView viewWithTag:SELECTION_INDICATOR_TAG_1]; indicatorO.image = [UIImage imageNamed:@"notSelected.png"]; oldCell.backgroundView.backgroundColor = [UIColor clearColor]; lastIndexPath = indexPath; } }

【问题讨论】:

    标签: cocoa-touch uitableview selected didselectrowatindexpath


    【解决方案1】:

    您正在重用具有图像“selected.png”的单元格。 在 cellForRowAtIndexPath 方法中,您必须根据您最后的选择“选择”或“取消选择”您的单元格。那就是:如果 indexPath 等于 lastIndexPath 你必须把背景“selected.png”,否则“noSelected.png”。 当您重用一个单元格时,它会保持其先前的状态,因此您必须将其全部初始化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      • 2011-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-03
      相关资源
      最近更新 更多