【问题标题】:Checkmark checks the row but not the object复选标记检查行但不检查对象
【发布时间】:2014-03-21 21:52:28
【问题描述】:

所以我在 UITableView 中使用 CoreData 和 NSUserDefault 的复选标记有两个问题

  1. 如果我在 tableview 中创建一个新对象,它是未选中的。当我检查这个对象时,它的检查。所以没关系......但是当我创建10个对象并检查第5个对象时,删除它并刷新tableView而不是第6个对象,现在是第5个对象,也被选中。我该如何解决这个问题?

  2. 当我删除一个选中的对象,并创建一个同名的对象,刷新 UITableView 时,它会在创建后被选中吗?什么? :D

我猜 NSUserDefault 是问题所在,但我不知道...

这是我的代码:

复选标记:

    - (BOOL) getCheckedForIndex:(int)index
    {
        if([[[NSUserDefaults standardUserDefaults] valueForKey:[self getKeyForIndex:index]] boolValue]==YES)
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }

    - (void) checkedCellAtIndex:(int)index
    {
        BOOL boolChecked = [self getCheckedForIndex:index];

        [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:!boolChecked] forKey:[self getKeyForIndex:index]];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }



    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [tableView deselectRowAtIndexPath:indexPath animated:NO];

        //Use checkedCellAtIndex for check or uncheck cell
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

        [self checkedCellAtIndex:indexPath.row];

        if([self getCheckedForIndex:indexPath.row]==YES)
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }

- (void) checkedCellAtIndex:(int)index
{
    BOOL boolChecked = [self getCheckedForIndex:index];

    [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:!boolChecked] forKey:[self getKeyForIndex:index]];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

表格视图:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cell_background.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
    cell.selectedBackgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cell_background.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];

    // Configure the cell...
    NSManagedObject *device = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView)
        {device = [searchResults objectAtIndex:indexPath.row];}
    else
        {device = [self.cart objectAtIndex:indexPath.row]; }

    [cell.textLabel setText:[NSString stringWithFormat:@"%@", [device valueForKey:@"cartProductName"]]];
    cell.tintColor = [UIColor orangeColor];
    if([self getCheckedForIndex:indexPath.row]==YES)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }


    return cell;
}

我希望有人可以帮助我。谢谢:)

【问题讨论】:

    标签: iphone uitableview checkmark


    【解决方案1】:

    回答您的 Q1:

        if([[[NSUserDefaults standardUserDefaults] valueForKey:[self getKeyForIndex:index]] boolValue]==YES)
    

    上面的代码检查NSUserDefaults 中的索引号。所以你保存tags的单元格对象/数组对象而不是数组索引号。

    编辑:由于您使用的是Coredata,因此您可以在模型类中添加BOOL 属性,例如:isChecked。 这样,您的数据库也将具有复选标记信息。使用相同的方法在tableview 中显示复选标记。 顺便说一句:为什么在使用CoreData 时使用NSUserDefaults。我的意思是它就像那句老话:Penny wise Pound Foolish

    【讨论】:

    • 啊,好吧,看起来是合法的。但是我需要改变什么?
    • @user255368 : 请看我上面的编辑
    • 我一直在考虑您关于使用 Core Data 保存复选标记的建议。我需要什么来设置它?我在这里找到了解决方案link,但他使用 NSFetchedResultscontroller 而我没有……这让我很难理解
    猜你喜欢
    • 2023-04-08
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    相关资源
    最近更新 更多