iOS表格视图单元格不';他没有表现出预期的行为

iOS表格视图单元格不';他没有表现出预期的行为,ios,reuseidentifier,Ios,Reuseidentifier,昨天我问了一个问题,关于一个单元格没有正确显示依赖于字符串值的按钮。如果需要,请查看: User@jrturton在回答中指出了以下几点: 一个被重用的单元每次都会添加子视图,这样就可以 有许多紧急的意见相互叠加。牢房应该永远 添加一次并将其保存在属性中 我认为这个答案标志着我解决问题必须遵循的正确方向,但我无法在我的代码中实现答案,如下所示: - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)ind

昨天我问了一个问题,关于一个单元格没有正确显示依赖于字符串值的按钮。如果需要,请查看:

User@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.
电池的性能必须符合以下要求:

- (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值时,才会发生此行为


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

您需要跟踪您的手机是否重复使用或是新手机

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

这样做

  • 再传递一个BOOL参数以配置单元格isFreshCell,其值是否根据新单元格设置
  • **

    新方法签名

    -(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath isFreshCell:(BOOL) isFreshCell
    
    2当您添加按钮时,请为其设置一些标记


    3如果isFreshCell为false,则不要添加新按钮,因为它已经存在,您可以使用subviewWithTag方法访问此按钮,如cell.contentView.subviewWithTag,然后设置图像或设置零图像或仅隐藏按钮,我同意@wuii,但我认为答案可以更清楚。其思想是,重用的单元格已经构建了它们的视图层次结构,因此每次重用单元格时(在滚动过程中始终如此)再次这样做是有害的。建议可以封装在一个“lazy getter”中,返回单元格的紧急按钮

    // 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];
    

    每当您想要比较Objective中字符串的内容时,请使用isEqualToString。再见,谢谢你的建议。我已将其更改为isEqualToString,但这并不能解决问题。谢谢,我如何跟踪它?非常感谢,您的答案很好。你让我开心。。。。我从你的回答中学到了很多。