UITableViewCellAccessoryCheckmark的逻辑 [英] Logic for UITableViewCellAccessoryCheckmark

查看:47
本文介绍了UITableViewCellAccessoryCheckmark的逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to make a typical situation: when user selects any cell, it's accessoryType turns in checkmark. Only one cell's accessoryType can be checkmark. And then I wanna save in NSUserDefaults indexPath.row so my app will be able to know which cell user selected and make some changes in options. So I wrote this wrong code:

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 单元格...我该如何改善代码或应该我会整体改变吗?有什么建议么 ?谢谢!

However, this code works badly. When you open the UITableView, there is an already selected cell in the table and when you press another there are two checkmarked cells...How can I improve my code or should I change it whole ? Any suggestions ? Thanks !

推荐答案

尝试以下代码:

- (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;
} 

这篇关于UITableViewCellAccessoryCheckmark的逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆