UITableView scrolling and redrawing issue

I know that if I have some images and subview added to a custom cell, then I have to reuse the cell so that the custom control does not appear in other cells, but here I have another problem. I just want to have an ImageView in the first cell of the first section, so I used IndexPath.Section == 0 and IndexPath.Row == 0 in the following code, but the problem is when I scroll through the table, the other cell will match this condition and mine the code will also create an image on this cell. I tried to tag it and use the same tagged cellView, but that didn't help either. The problem with the cell is to disable custom interactions for multiple cells. Eventually after scrolling, it disables user interaction for all cells. Is there a way to resolve this?

Thank.

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

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}

if(indexPath.section == 0 && indexPath.row == 0) {
    UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"me.jpg"]] autorelease];
    UIView *cellView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,320,132)] autorelease];
    [imageView setFrame: CGRectMake(10, 10, 54, 54)];
    [cellView addSubview:imageView];
    cell.backgroundView = cellView;

    return cell;
} else if(indexPath.row == 0) {
    NSString * title = [NSString string];
    switch (indexPath.section) {
        case 1:
            title = @"Friends";
            break;
        case 2:
            title = @"Accounts";
            break;
        case 3:
            title = @"Stats";
            break;
        default:
            title = nil;
            break;
    }
    cell.textLabel.text = title;
    cell.userInteractionEnabled = NO;
    return cell;
}

cell.textLabel.text = @"Test";
return cell;
}

      

[RESOLVED] Correct code:

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

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) 
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];

if(indexPath.section == 0 && indexPath.row == 0) {
    UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"me.jpg"]] autorelease];
    cell.imageView.image = imageView.image;
    cell.textLabel.text = nil;
    cell.textLabel.textColor = [UIColor clearColor];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.userInteractionEnabled = YES;
    return cell;
} else if(indexPath.row == 0) {
    NSString * title = [NSString string];
    switch (indexPath.section) {
        case 1:
            title = @"Friends";
            break;
        case 2:
            title = @"Accounts";
            break;
        case 3:
            title = @"Stats";
            break;
        default:
            title = nil;
            break;
    }

    cell.imageView.image = nil;
    cell.textLabel.text = title;
    cell.textLabel.textColor = [UIColor redColor];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.userInteractionEnabled = NO;

    return cell;
}


cell.imageView.image = nil;
cell.textLabel.text = [cellItems objectAtIndex:(rows+indexPath.row-1)];
cell.textLabel.textColor = [UIColor blueColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.userInteractionEnabled = YES;
return cell;
}

      

[IMPROVED CODE]

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

static NSString *NormalCellIdentifier = @"NormalCell";
static NSString *TitleCellIdentifier = @"TitleCell";
NSString *neededCellType;

if(indexPath.section == 0 && indexPath.row == 0) {
    neededCellType = TitleCellIdentifier;
} else {
    neededCellType = NormalCellIdentifier;
}

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:neededCellType];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:neededCellType] autorelease];

    //Only add content to cell if it is new
    if([neededCellType isEqualToString: TitleCellIdentifier]) {
        UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"me.jpg"]] autorelease];
        cell.imageView.image = imageView.image;
    }
}

if([neededCellType isEqualToString: NormalCellIdentifier]) {
    NSString * title;
    if(indexPath.row == 0) {
        switch (indexPath.section) {
            case 1:
                title = @"Friends";
                break;
            case 2:
                title = @"Accounts";
                break;
            case 3:
                title = @"Stats";
                break;
            default:
                title = nil;
                break;
        }
        cell.textLabel.text = title;
        cell.textLabel.textColor = [UIColor redColor];
        cell.userInteractionEnabled = NO;
    } else {

        cell.userInteractionEnabled = YES;
        cell.textLabel.textColor = [UIColor blueColor];
        cell.textLabel.text = @"Test";
    }
}

return cell; 
}

      

+1


source to share


2 answers


The problem is that you don't allow the possibility that the cell that displayed the image correctly will be reused later and the image will still be there.

Here are two solutions:

  • set the value of the image tag when you create it, and then when customizing the cells, include the code to check and delete the old image.

  • assign different reuse IDs to cells that need to view images and those that don't. Then make sure that you only add the new image view to the cells when they are created, not when they are reused.

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

static NSString * NormalCellIdentifier = @ "NormalCell"; static NSString * TitleCellIdentifier = @ "TitleCell"; NSString * requiredCellType;

if (indexPath.section == 0 && indexPath.row == 0) {requiredCellType = TitleCellIdentifier; } else {requiredCellType = NormalCellIdentifier; }

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: neededCellType];



if (cell == nil) {cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: neededCellType] autorelease];

// Only add content to the cell if it is new if ([requiredCellType isEqualToString: TitleCellIdentifier]) {UIImageView * imageView = [[[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "me.jpg"]] autorelease] ; UIView * cellView = [[[UIView alloc] initWithFrame: CGRectMake (0, 0,320,132)] autorelease]; [imageView setFrame: CGRectMake (10, 10, 54, 54)]; [cellView addSubview: imageView]; cell.backgroundView = cellView; }}

if ([requiredCellType isEqualToString: NormalCellIdentifier]) {NSString * title; if (indexPath.row == 0) {

switch (indexPath.section) {
    case 1:
        title = @"Friends";
        break;
    case 2:
        title = @"Accounts";
        break;
    case 3:
        title = @"Stats";
        break;
    default:
        title = nil;
        break;
}
cell.textLabel.text = title;
cell.userInteractionEnabled = NO;

      

}

else { cell.textLabel.text = @"Test"; return cell; } } }

(The last few lines fell out of the code). This should do it.

0


source


I think your problem is that reusing cells makes it so cells that are not created as new cells have some properties that you must override. For example, try assigning cell.userInteractionEnabled = YES for all other cases and see what the result is.



+1


source







All Articles