0

我在 TableView 中有一个产品列表。该列表由 WCF 调用填充。我可以在日志中看到产品正在加载正确的值。其中一个值是一个名为 Bought 的布尔值。我想用它来显示(在 tableviewCell 上打勾),如果产品被购买或没有

在这里我加载单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ProductCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    id prod = [_productList objectAtIndex:indexPath.row];

    cell.textLabel.text = [prod ProductName];
    NSString *stringAmount = [NSString stringWithFormat:@"%@", [prod Amount]];
    cell.detailTextLabel.text = stringAmount;
    if (![prod Bought]) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }


    return cell;
}

在这里我改变了价值

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {    
    [self markBought];
    [tableView reloadData];
    [self reloadInputViews];
}

didSelectRowAtIndexPath方法有效(我可以看到数据库中的值发生变化)但复选标记始终打开 -

我怀疑我加载 Accessorytype 错误 - 但我真的不知道如何以不同的方式进行操作。

4

1 回答 1

1

[prod Bought]返回一个NSNumber对象,因此您必须替换

if (![prod Bought]) ...

经过

if (![[prod Bought] boolValue]) ...
于 2013-06-02T14:25:06.303 回答