重新加载UITableView会导致标签设置不正确

时间:2014-11-17 21:02:57

标签: ios objective-c uitableview

当我的UITableView第一次加载时,下面代码中的所有内容都能正常运行。但是,如果因任何原因重新加载(刷新等),它会开始为随机单元格分配cell.bestMatchLabel.text @"Best Match"值,而不是像我在代码中指定的那样只分配第一个值。为什么在我的表上调用reload导致下面的代码无法正常运行?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //load top 3 data
    NSDictionary *currentSectionDictionary = _matchCenterArray[indexPath.section];
    NSArray *top3ArrayForSection = currentSectionDictionary[@"Top 3"];

    // if no results for that item
    if (top3ArrayForSection.count-1 < 1) {

        // Initialize cell
        static NSString *CellIdentifier = @"MatchCenterCell";
        EmptyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (!cell) {
            // if no cell could be dequeued create a new one
            cell = [[EmptyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }

        // title of the item
        cell.textLabel.text = @"No items found, but we'll keep a lookout for you!";
        cell.textLabel.font = [UIFont systemFontOfSize:12];

        cell.detailTextLabel.text = [NSString stringWithFormat:@""];
        [cell.imageView setImage:[UIImage imageNamed:@""]];

        return cell;
    }

    // if results for that item found
    else {

        // Initialize cell
        static NSString *CellIdentifier = @"MatchCenterCell";
        MatchCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (!cell) {
            // if no cell could be dequeued create a new one
            cell = [[MatchCenterCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }

        tableView.separatorColor = [UIColor clearColor];

        if (indexPath.row == 0) {
            cell.bestMatchLabel.text = @"Best Match";
            cell.bestMatchLabel.font = [UIFont systemFontOfSize:12];
            cell.bestMatchLabel.textColor = [UIColor colorWithRed:0.18 green:0.541 blue:0.902 alpha:1];

            [cell.contentView addSubview:cell.bestMatchLabel];
        }


        // title of the item
        cell.textLabel.text = _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Title"];
        cell.textLabel.font = [UIFont systemFontOfSize:14];

        // price + condition of the item
        NSString *price = [NSString stringWithFormat:@"$%@", _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Price"]];
        NSString *condition = [NSString stringWithFormat:@"%@", _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Item Condition"]];

        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@", price, condition];
        cell.detailTextLabel.textColor = [UIColor colorWithRed:0.384 green:0.722 blue:0.384 alpha:1];

        // Load images using background thread to avoid the laggy tableView
        [cell.imageView setImage:[UIImage imageNamed:@"Placeholder.png"]];

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
            // Download or get images here
            NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Image URL"]]];

            // Use main thread to update the view. View changes are always handled through main thread
            dispatch_async(dispatch_get_main_queue(), ^{
                // Refresh image view here
                [cell.imageView setImage:[UIImage imageWithData:imageData]];
                cell.imageView.layer.masksToBounds = YES;
                cell.imageView.layer.cornerRadius = 2.5;
                [cell setNeedsLayout];
            });
        });
        return cell;
    }

}

1 个答案:

答案 0 :(得分:1)

这是因为当您向下滚动时,表会为多个索引出列相同的单元格,因此您需要在以下代码中添加else语句

if (indexPath.row == 0) {
        cell.bestMatchLabel.text = @"Best Match";
        cell.bestMatchLabel.font = [UIFont systemFontOfSize:12];
        cell.bestMatchLabel.textColor = [UIColor colorWithRed:0.18 green:0.541 blue:0.902 alpha:1];

        [cell.contentView addSubview:cell.bestMatchLabel];
    [cell.bestMatchLabel setHidden:NO];
} else {
    [cell.bestMatchLabel setHidden:YES];
}

但是对于这种情况更好的方法是为该行使用不同的单元格标识符,并且在首次创建单元格时仅添加bestMatchLabel