开发者

Why the UITableViewCell's changing their value in single cell?

In my app, i have customize UITableViewCell and is using its contentView property. But now the problem is the data in cells are sometimes going in different cells. For example:The data in cell 3 is going into cell 8 or 9 when scroll to the bottom or scroll up. Here's the code which iam using in cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";
    CGFloat fontSize = [UIFont systemFontSize];
    CGFloat smallFontSize = [UIFont smallSystemFontSize];
    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]autorelease];
        mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(5,0,150,42)] autorelease];
        mainLabel.font = [UIFont systemFontOfSize:smallFontSize];
        mainLabel.numberOfLines = 3;
        mainLabel.lineBreakMode = UILineBreakModeWordWrap;
        mainLabel.backgroundColor = [UIColor clearColor];
        mainLabel.autoresizingMask = UIViewAutoresizingF开发者_如何学JAVAlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
        [cell.contentView addSubview:mainLabel];

        secondLabel = [[[UILabel alloc] initWithFrame:CGRectMake(160,0,160,42)] autorelease];
        secondLabel.font = [UIFont systemFontOfSize:smallFontSize];
        secondLabel.numberOfLines = 3;
        secondLabel.lineBreakMode = UILineBreakModeWordWrap;
        secondLabel.backgroundColor = [UIColor clearColor];
        secondLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
        [cell.contentView addSubview:secondLabel];


    }
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont systemFontOfSize:smallFontSize];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:smallFontSize];
    NSArray * titles = [[NSArray alloc] initWithObjects:@"Project ID", @"Status", @"Approval Date", @"Closing Date", @"Country",@"Region Name", @"Env", @"Team Full Name",@"Borrower", @"Impagency", @"Lending Cost", @"IBRDPlus", nil];

    switch (indexPath.section) {
        case 0:

            mainLabel.text = [titles objectAtIndex:indexPath.row];
            mainLabel.adjustsFontSizeToFitWidth = YES;
            mainLabel.numberOfLines = 4;
            mainLabel.lineBreakMode = UILineBreakModeWordWrap;

            secondLabel.text = [self.basic objectAtIndex:indexPath.row];
            secondLabel.adjustsFontSizeToFitWidth = YES;
            secondLabel.numberOfLines = 4;
            secondLabel.lineBreakMode = UILineBreakModeWordWrap;
            break;
        case 1:

            mainLabel.text = [[self.allSectors objectAtIndex:indexPath.row] valueForKey:@"SECTORNAME"];
            mainLabel.adjustsFontSizeToFitWidth = YES;
            mainLabel.numberOfLines = 4;
            mainLabel.lineBreakMode = UILineBreakModeWordWrap;

            secondLabel.text = [[self.allSectors objectAtIndex:indexPath.row] valueForKey:@"SECTORPCT"];
            secondLabel.adjustsFontSizeToFitWidth = YES;
            secondLabel.numberOfLines = 4;
            secondLabel.lineBreakMode = UILineBreakModeWordWrap;

            break;
        case 2:

            mainLabel.text = [[self.themes objectAtIndex:indexPath.row] valueForKey:@"THEME_NAME"];
            mainLabel.adjustsFontSizeToFitWidth = YES;
            mainLabel.numberOfLines = 4;
            mainLabel.lineBreakMode = UILineBreakModeWordWrap;

            secondLabel.text = [[self.themes objectAtIndex:indexPath.row] valueForKey:@"THEMEPCT"];
            secondLabel.adjustsFontSizeToFitWidth = YES;
            secondLabel.numberOfLines = 4;
            secondLabel.lineBreakMode = UILineBreakModeWordWrap;

            break;

        default:
            break;
    }

    return cell;
}


This is happening because of this line (which you need to have anyway) :

UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];

Basically because the cells are being reused the content stays in the cell, therefore you need to clean that cell after dequeu and then set the new contentView.

What is happen here specifically is that even though you are resetting the text of secondLabel, when you are setting your variable here:

secondLabel = [[[UILabel alloc] initWithFrame:CGRectMake(160,0,160,42)] autorelease];

The last cell that was created is what secondLabel is pointing to all the time, so you are really always changing the same label, therefore I would suggest something like this:

UILabel *lblCell;
for (id label in [cell.contentView subviews]) {
            if ([label isKindOfClass:[UILabel class]]) {
                lblCell = label;
            }
        }

lblCell.text = [[self.themes objectAtIndex:indexPath.row] valueForKey:@"THEMEPCT"];

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜