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

时间:2015-03-25 22:05:02

标签: ios objective-c uitableview tableviewcell

我的UITableView显示出一些非常奇怪的行为。

以下是正在发生的事情的图片:

enter image description here

我通过添加一些信息来测试填充UITableView。每个单元格的每一次我都将事件更新为目标,并且玩家名称为 tet 。是的,每次即使你可以看到的最后3个细胞显示出一些奇怪的行为。

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

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

这是另一个例子:

enter image description here

在这个例子中,我决定每隔一次让每一方得分为#34; 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;
}

1 个答案:

答案 0 :(得分:2)

你的问题是你没有考虑细胞被重复使用的事实。当您获得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,但在另一种情况下不设置它。

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

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