0

昨天我问了一个关于单元格没有正确显示取决于字符串值的按钮的问题。如果需要,请看一下:带有核心数据的表格视图的奇怪行为

用户@jrturton 在他的回答中指出以下内容:

重复使用的单元格每次都会添加子视图 - 因此可能会有许多紧急视图相互叠加。单元格应该只添加一次并将其保存在属性中

我认为这个答案标志着我必须遵循以解决我的问题的正确方向,但我无法将答案实施到我的代码中,如下所示:

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObject *managedObject = [fetchedResultsController   objectAtIndexPath:indexPath];

    NSString *isUrgent = [[managedObject valueForKey:@"urgent"]description];


    [[cell textLabel] setText:[[managedObject valueForKey:@"thingName"] description]];


    //urgent

    if ([isUrgent isEqual:@"Urgent"]){
    UIButton *urgentButton = [[UIButton alloc]initWithFrame:CGRectMake(71, 27, 18, 18)];
    [urgentButton setImage:[UIImage imageNamed:@"urgent-3"]forState:UIControlStateNormal];
    [cell addSubview:urgentButton];
        NSLog(isUrgent);
    }
    //not urgent 
    if ([isUrgent isEqual:@"Not urgent"]){
        UIButton *urgentButton = [[UIButton alloc]initWithFrame:CGRectMake(71, 27, 18, 18)];
        [urgentButton setImage:[UIImage imageNamed:nil]forState:UIControlStateNormal];
        [cell addSubview:urgentButton];
        NSLog(isUrgent);
    }


    [[cell detailTextLabel] setText:@"  "];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.textLabel.textColor = [UIColor blueColor];
    cell.textLabel.font = [UIFont fontWithName:@"Noteworthy" size:22.0f];
    cell.detailTextLabel.font = [UIFont fontWithName:@"Noteworthy" size:15.0f];

}

单元格的行为必须如下:

1. If isUrgent = @"Urgent", the cell must show urgentButton (including imageNamed:@"urgent-3":
2. Else no button has to be shown.

当前行为如下:

1. If isUrgent = @"Urgent", the cell shows urgentButton (including imageNamed:@"urgent-3".
2. If isUrgent = @"Not urgent", value tested in NSLog, the cell shows urgentButton too.

此行为仅在单元格至少更改一次其 isUrgent 值时发生。

我需要您的帮助来实施上述答案。谢谢你。

4

2 回答 2

1

您需要跟踪您的电池是否被重复使用或是否是新的。

这里可能发生的情况是您正在将 UIButton 添加到重用单元格,但它已经有按钮设置了“urgent-3”图像

做这个

  1. 再传递一个 BOOL 参数来配置单元格 isFreshCell 是否根据新单元格设置值。

**

MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
isFreshCell = NO;
if(cell==nil)
{
 isFreshCell = YES;
 cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
} 
[self configureCell:cell atIndexPath:indexPath isFreshCell:isFreshCell];

新方法签名

-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath isFreshCell:(BOOL) isFreshCell

2 当您添加按钮时,为其设置一些标签。

3 如果 isFreshCell 为假,请不要添加新按钮,因为它已经存在,您可以使用这样的 subviewWithTag 方法访问此按钮cell.contentView.subviewWithTag,然后设置图像或设置 nil 图像或只是隐藏按钮

于 2013-12-26T19:30:27.210 回答
1

我同意@wuii,但我认为答案可以更清楚。这个想法是重用的单元格已经构建了它们的视图层次结构,因此每次重用单元格时再次执行它是有害的(这是滚动期间的所有时间)。建议可以封装在返回单元格的紧急按钮的“惰性吸气剂”中。

// above @implementation
#define kURGENT_BUTTON_TAG  (256)

- (UIButton *)urgentButtonInCell:(UITableViewCell *)cell {

    UIButton *urgentButton = (UIButton *)[cell viewWithTag:kURGENT_BUTTON_TAG];
    if (!urgentButton) {
        urgentButton = [[UIButton alloc]initWithFrame:CGRectMake(71, 27, 18, 18)];
        urgentButton.tag = kURGENT_BUTTON_TAG;
        [cell addSubview:urgentButton];
    }
    return urgentButton;
}

现在您的 configureCell 可以只要求按钮:

UIButton *urgentButton = [self urgentButtonInCell:cell];
UIImage *image = ([isUrgent isEqualToString:@"Urgent"])? [UIImage imageNamed:@"urgent-3"] : nil;
[urgentButton setImage:image forState:UIControlStateNormal];
于 2013-12-26T19:51:12.703 回答