2012-04-03 89 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变量添加到你的类的头部,或者将它变成一个属性或其他东西。