iphone - UITableViewCellAccessoryCheckmark的逻辑

标签 iphone objective-c ios xcode uitableview

我想做一个典型的情况:当用户选择任何单元格时,其附件类型将变为选中标记。只能将一个单元格的annexType选中。然后,我想保存在NSUserDefaults indexPath.row中,以便我的应用程序能够知道选择了哪个单元用户并在选项中进行了一些更改。所以我写了这个错误的代码:

didSelectRowAtIndexPath

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

    if(self.checkedIndexPath)
    {
        UITableViewCell* uncheckCell = [tableView
                                        cellForRowAtIndexPath:self.checkedIndexPath];
        uncheckCell.accessoryType = UITableViewCellAccessoryNone;
    }
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    self.checkedIndexPath = indexPath;

    [[NSUserDefaults standardUserDefaults]setObject:[NSNumber numberWithInt:self.checkedIndexPath.row]forKey:@"indexpathrow" ];
} 

cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Part of code from cellForRowAtIndexPath

    if(indexPath.row == [[[NSUserDefaults standardUserDefaults]objectForKey:@"indexpathrow"]intValue ])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else 
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

        return cell;
}

但是,此代码效果很差。当您打开UITableView时,表中已经有一个选定的单元格,当您按下另一个单元格时,有两个checkmarked单元格...我该如何改善代码或应该整体更改代码?有什么建议么 ?
谢谢 !

最佳答案

试试这个代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // checkedIndexPath is NSIndexPath
    NSIndexPath *previousSelection = self.checkedIndexPath;
    NSArray *array = nil;
    if (nil != previousSelection) {
        array = [NSArray arrayWithObjects:previousSelection, indexPath, nil];
    } else {
        array = [NSArray arrayWithObject:indexPath];
    }

    self.checkedIndexPath = indexPath;

    [tableView reloadRowsAtIndexPaths:array withRowAnimation: UITableViewRowAnimationNone];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Part of code from cellForRowAtIndexPath
    NSString *cellID = @"CellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        [cell autorelease];
    }

// some code for initializing cell content
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if(self.checkedIndexPath != nil && indexPath.row == self.checkedIndexPath.row)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
} 

关于iphone - UITableViewCellAccessoryCheckmark的逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10622102/

相关文章:

ios - 我希望我的应用程序能够识别 iPhone 型号,因为我没有使用自动布局

ios - 如何使用 FileManager 检索嵌套文件?

ios - Xcode 8.2.1中如何拖放一个可以自动展开满屏的UITableView

ios - meteor Cordova 构建与生产服务器(无热代码推送)

ios - 无法在属性初始值设定项中使用实例成员 'server'

iphone - 如何在 iOS 中使用位图/rgb 数据为图像制作动画

ios - UIButton 将文本设置在图像顶部

iphone - 触摸移动时将底 View 移至顶 View

objective-c - static const int 对于数组大小来说不够好?

iphone - ASIHttpRequest 替换和图像缓存