UITableView,在选择时更改配件时出现问题

zpa*_*ack 0 iphone objective-c uitableview

我正在使用UITableView来选择一个(很多)项目.与选择铃声时的UI类似,我想要检查所选项目,而不是其他项目.我想在触摸时选择单元格,然后将其设置为正常颜色(再次,就像铃声选择UI一样).

UIViewController子类是我的表的委托和数据源(不是UITableViewController,因为我还有一个工具栏).

我在cellForRowAtIndexPath:中设置单元格的accessoryType,并在didSelectRowAtIndexPath:中选择单元格时更新我的​​模型.我可以想到将所选单元格设置为复选标记(并清除前一个单元格)的唯一方法是在didSelectRowAtIndexPath:中调用[tableView reloadData].但是,当我这样做时,单元格取消选择的动画很奇怪(单元格的标签应该出现一个白框).当然,如果我不调用reloadData,则accessoryType不会更改,因此不会出现复选标记.

我想我可以关闭动画,但这似乎很蹩脚.我也玩弄了改变didSelectRowAtIndexPath:中的细胞,但这是一个很大的痛苦.

有任何想法吗?缩写代码如下......

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:kImageCell];
    if( aCell == nil ) {
        aCell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:kImageCell];
    }

    aCell.text = [imageNames objectAtIndex:[indexPath row]];
    if( [indexPath row] == selectedImage ) {
        aCell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        aCell.accessoryType = UITableViewCellAccessoryNone;
    }
    return aCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    selectedImage = [indexPath row]
    [tableView reloadData];
}
Run Code Online (Sandbox Code Playgroud)

Ron*_*bro 8

我像他的编辑中提到的hatfinch一样解决了这个问题.我的代码示例确保只检查了一行,但我确信如果不是您需要的话,您可以调整它.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

int newRow = [indexPath row];
int oldRow = [lastIndexPath row];

if (newRow != oldRow)
{
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
                                indexPath];
    newCell.accessoryType = UITableViewCellAccessoryCheckmark;

    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:
                                lastIndexPath];
    oldCell.accessoryType = UITableViewCellAccessoryNone;

    lastIndexPath = indexPath;
}

[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Run Code Online (Sandbox Code Playgroud)