我仍然在熟悉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;
}
}
答案 0 :(得分:2)
您正在重复使用图片“selected.png”的单元格。 在方法cellForRowAtIndexPath中,您必须根据上次选择“选择”或“取消选择”您的单元格。也就是说:如果indexPath等于lastIndexPath,则必须将背景设置为“selected.png”,否则设置为“noSelected.png”。 当您重用一个单元格时,它会保持其先前的状态,因此您必须将其全部初始化。