2012-04-03 67 views
0

在我的iPhone應用程序中,如果對象的isConfirmed值爲true,我會在表格視圖中爲單元格添加刻度圖像。當輸入詳細視圖時,我可以編輯確認的值,並且在彈出回主表視圖時,我需要看到更新,而不僅僅是當我從新的主視圖查看主表時。從父視圖中移除圖像

所以我用我的tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method`驗證碼:

UIImageView *tickImg = nil; 

    //If confirmed add tick to visually display this to the user 
    if ([foodInfo.isConfirmed boolValue]) 
    { 
     tickImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ConfirmedTick.png"]]; 
     [tickImg setFrame:CGRectMake(0, 0, 32, 44)]; 
     [cell addSubview:tickImg]; 
    } 
    else 
    { 
     [tickImg removeFromSuperview]; 
    } 

這裏做的事情是成功的蜱圖像添加到我的細胞,其具有isConfirmed真值和進入的細節視圖時一個對象,並將其設置爲TRUE並重新調整,則會出現勾號,但是我無法使其工作,因此,如果勾號存在,並且我進入詳細視圖以確認它,則勾號不會消失。

希望你能幫助,我這個,謝謝。

回答

0

你在調用[self.tableView reloadData];在VC的視角上會出現:?

此外,您用於配置單元格的方法很容易出錯。由於tableView重複使用單元格,因此在出列單元格時,無法確定單元格的狀態。

一個更好的方法是一致地構建細胞:

static NSString *CellIdentifier = @"MyCell"; 
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    // always create a tick mark 
    UIImageView *tickImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ConfirmedTick.png"]]; 
    tickImg.tag = kTICK_IMAGE_TAG; 
    tickImg.frame = CGRectMake(0, 0, 32, 44); 
    [cell addSubview:tickImg]; 
} 

// always find it 
UIImageView *tickImg = (UIImageView *)[cell viewWithTag:kTICK_IMAGE_TAG]; 

// always show or hide it based on your model 
tickImg.alpha = ([foodInfo.isConfirmed boolValue])? 1.0 : 0.0; 

// now your cell is in a consistent state, fully initialized no matter what cell 
// state you started with and what bool state you have 
+0

想必您的代碼示例中你必須在Interface Builder中你的形象有何看法?因爲您不會將其作爲子視圖添加。 – 2012-04-03 23:11:33

+0

不,我喜歡這種方式,但是你的代碼表明你在代碼中構建了圖像視圖,所以我在答案中也這樣做了。看見? – danh 2012-04-03 23:16:58

+0

oops。我沒有把它作爲子視圖添加。我的錯。將編輯。現在就試試這個代碼。我認爲它會做你想做的。 – danh 2012-04-03 23:17:26

1

這是執行,如果[foodInfo.isConfirmed boolValue]是假的代碼:

UIImageView *tickImg = nil; 
[tickImg removeFromSuperview]; 

顯然,這是行不通的 - tickImg沒有指向的UIImageView。你需要以某種方式保存對UIImageView的引用。你可以將tickImg變量添加到你的類的頭部,或者將它變成一個屬性或其他東西。