UITableViewCell 有时显示正确的信息有时错误?

UITableViewCell sometimes showing right information sometimes wrong?

我的 UITableView 出现了一些非常奇怪的行为。

这是正在发生的事情的图片:

我正在测试通过添加一些信息来填充 UITableView。对于每个单元格,我每次都将事件更新为 Goal,将玩家名称更新为 tet。是的,即使您看到的最后 3 个单元格每次都显示出一些奇怪的行为。

在“10-0”比分之后,我决定让另一支球队用同名的 tet 得分。第一次它在两侧显示图像并且未显示名称。第二次显示正确,第三次显示错误。

但是 如果我导航到另一个视图然后返回,那么一切看起来都是正确的。

这是另一个例子:

在这个例子中,我决定让每一方每隔一秒得分一次,名称为"twfr"。如您所见,它在 5-2 得分单元格中出错。

我不知道是什么原因造成的。我尝试使用此代码

[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];

在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

它确实有效,但您可以清楚地看到它何时出错,但它会迅速更改并显示错误此外,它在滚动时非常滞后。那么请问大家可能是什么导致了这个问题?

EDIT2 发布更多代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {

        static NSString *identifier = @"MainCell";

        GamesInfoTableViewCell *cell = (GamesInfoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];

        cell.userInteractionEnabled = NO;

        //cell.backgroundColor = detailCol;


        [cell.homeTeamLabel setFont:[UIFont boldSystemFontOfSize:16]];
        [cell.awayTeamLabel setFont:[UIFont boldSystemFontOfSize:16]];

        cell.gameTimeLabel.textColor = TEXT;
        cell.sectionName.textColor = TEXT;

        cell.liveButton.image = [UIImage imageNamed:@"LiveButton.png"];

        if ([_GameInfoDictionary[@"time"] isEqualToString:@"FT"])
            cell.liveButton.hidden = TRUE;

        cell.sectionName.text = self.selectedSection;
        cell.homeTeamLabel.text = [self.GameInfoDictionary objectForKey:@"homeTeam"];
        cell.awayTeamLabel.text = _GameInfoDictionary[@"awayTeam"];
        cell.gameTimeLabel.text = _GameInfoDictionary[@"time"];
        cell.homeTeamScoreLabel.text = _GameInfoDictionary[@"homeScore"];
        cell.awayTeamScoreLabel.text = _GameInfoDictionary[@"awayScore"];

        return cell;
    }

    // If its the other section

    static NSString *otherSection = @"events";

    GamesInfoTableViewCell *cell1 = (GamesInfoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:otherSection];

    cell1.backgroundColor = TABLECOLOR;
    // Configure the cell...
    cell1.gameTimeLabel.textColor = TEXT;
    cell1.homeNameLabel.textColor = TEXT;
    cell1.awayNameLabel.textColor = TEXT;



    // Select the event object for the specefik row
    NSMutableDictionary *gameInfoObject =[_events objectAtIndex:indexPath.row];

    // Seperate event and name
    NSArray * array = [gameInfoObject[@"eventType"] componentsSeparatedByString:@"-"];
    NSString * typeEvent = [array objectAtIndex:0];
    NSString * nameOfPlayer = [array objectAtIndex:1];

    UIImage *imageType;

    //Change the typeofevent name here to goal so it matches with the image name,
    // Set the name of the event same as the picture to load correct image
    NSMutableString *eventNameToImage = [NSMutableString string];
    [eventNameToImage appendString:typeEvent];
    [eventNameToImage appendString:@".png"];

    imageType = [UIImage imageNamed:eventNameToImage];

    if ([typeEvent isEqualToString:goal]  || [typeEvent isEqualToString:penaltyGoal] || [typeEvent isEqualToString:owngoal] )
        cell1.bothScoreLabel.text = gameInfoObject[@"eventScore"];

    else
        cell1.bothScoreLabel.hidden = YES;

    cell1.gameTimeLabel.text = gameInfoObject[@"eventTime"];

    if ([gameInfoObject[@"selectedTeam"] isEqualToString:@"homeTeam"])
    {
        cell1.homeNameLabel.text = nameOfPlayer;
        cell1.awayNameLabel.hidden = YES;
        cell1.homeTeamImage.image = imageType;

        if([typeEvent isEqualToString:owngoal]){
            [cell1.homeOwnGoalLabel setFont:[UIFont boldSystemFontOfSize:7]];
            cell1.homeOwnGoalLabel.hidden = NO;
        }

        else if([typeEvent isEqualToString:penaltyGoal] || [typeEvent isEqualToString:penaltyMiss])
            cell1.homeNameLabel.text = [cell1.homeNameLabel.text stringByAppendingString:@" (str.)"];
    }

    else
    {
        cell1.awayNameLabel.text = nameOfPlayer;
        cell1.homeNameLabel.hidden = YES;
        cell1.awayTeamImage.image = imageType;

        if([typeEvent isEqualToString:owngoal]){
            [cell1.awayOwnGoalLabel setFont:[UIFont boldSystemFontOfSize:7]];
            cell1.awayOwnGoalLabel.hidden = NO;
        }

        else if([typeEvent isEqualToString:penaltyGoal] || [typeEvent isEqualToString:penaltyMiss])
            cell1.awayNameLabel.text = [cell1.awayNameLabel.text stringByAppendingString:@" (str.)"];
    }

    //[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];

    return cell1;
}

你的问题是你没有考虑细胞被重复使用的事实。当您收到 cell 时,它可能已经有上一次调用的数据集。

例如,这里:

if ([gameInfoObject[@"selectedTeam"] isEqualToString:@"homeTeam"])
{
    cell1.homeNameLabel.text = nameOfPlayer;
    cell1.awayNameLabel.hidden = YES;
    cell1.homeTeamImage.image = imageType;

    if([typeEvent isEqualToString:owngoal]){
        [cell1.homeOwnGoalLabel setFont:[UIFont boldSystemFontOfSize:7]];
        cell1.homeOwnGoalLabel.hidden = NO;
    }

    else if([typeEvent isEqualToString:penaltyGoal] || [typeEvent isEqualToString:penaltyMiss])
        cell1.homeNameLabel.text = [cell1.homeNameLabel.text stringByAppendingString:@" (str.)"];
}

else
{
    cell1.awayNameLabel.text = nameOfPlayer;
    cell1.homeNameLabel.hidden = YES;
    cell1.awayTeamImage.image = imageType;

    if([typeEvent isEqualToString:owngoal]){
        [cell1.awayOwnGoalLabel setFont:[UIFont boldSystemFontOfSize:7]];
        cell1.awayOwnGoalLabel.hidden = NO;
    }

    else if([typeEvent isEqualToString:penaltyGoal] || [typeEvent isEqualToString:penaltyMiss])
        cell1.awayNameLabel.text = [cell1.awayNameLabel.text stringByAppendingString:@" (str.)"];
}

你在一种情况下设置了homeTeamImage,但在另一种情况下不设置它。

您需要在所有情况下设置所有变量,否则它们将保留上次显示此单元格对象时的值。

或者,您可以在 UITableViewCell 子类中实现 prepareForReuse 并将所有内容重置为默认值,但这可能会影响性能。