0

在我的 iPhone 应用程序中,我有一个表格视图,如果该对象的“isConfirmed”值为真,我会在其中向单元格添加刻度图像。进入详细视图时,我可以编辑确认的值,并且在弹出回主表视图时,我需要查看更新,而不仅仅是在我重新查看主表时。

所以我在我的方法中使用了这段代码tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

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 并重新调整时,刻度出现,但是我无法让它工作其他,所以如果有勾号并且我进入详细视图未确认它,勾号不会消失。

4

2 回答 2

1

这是如果[foodInfo.isConfirmed boolValue]为 false 时执行的代码:

UIImageView *tickImg = nil;
[tickImg removeFromSuperview];

显然这不起作用——tickImg 没有指向 UIImageView。您需要以某种方式保存对 UIImageView 的引用。您可以将 tickImg 变量添加到类的标题或使其成为属性或其他东西。

于 2012-04-03T22:12:42.747 回答
0

你在打电话 [self.tableView reloadData]; 对VC的看法WillAppear:?

此外,您用于配置单元的方法容易出错。由于 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
于 2012-04-03T22:12:43.527 回答