2013-06-02 23 views
0

我有一個TableView中的產品列表。該列表由WCF調用填充。我可以在日誌中看到產品正在加載正確的值。其中一個值是一個名爲Bought的布爾值。我想用它來顯示(與上tableviewCell標記),如果產品是買還是不單元格上的accessoryType不變

在這裏,我裝入電池:

- (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錯誤 - 但我真的不知道如何做不同。

+0

取代

if (![prod Bought]) ... 

甲產品出來這樣的: ' 1Æg'' – catu

+0

'[prod Bou ght]'返回一個標量(例如'BOOL')還是一個對象(例如'NSNumber')?在後一種情況下,您可能需要檢查'if(![[prod bought] boolValue]){...}' –

+0

完美 - 謝謝:) – catu

回答

1

[prod Bought]返回NSNumber對象,因此必須通過

if (![[prod Bought] boolValue]) ... 
+0

我的選中標記沒有更新。我必須離開列表並再次輸入以查看更新的選中標記。 我需要在'didSelectRowAtIndexPath'方法中執行除[[self tableview reloadData]以外的任何操作嗎? – catu

+0

解決了它。 需要'UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath]; if(cell.accessoryType == UITableViewCellAccessoryNone)cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; }' – catu

相關問題