1

I know that if I have some images and subviews added in customized cell then I have to reuse the cell so that custom control won't appear on other cells but here I have other issue. I just want to have ImageView on first cell of first section so I have used IndexPath.Section==0 and IndexPath.Row==0 condition in following code but the problem is when I scroll table, the other cell will meet this condition and my code will create imageview on that cell as well. I have tried Tagging it and using same tagged cellView but it didn't help either. The cell issue is with disabling user interactions for few cells. Eventually after scrolling it disables user interactions for all cells. Is there anyway to resolve this?

Thanks.

- (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; 
}

2 Answers 2

1

I think your problem is that the reuse of cells makes it so that the cells that aren't being created as new cells have some properties set that you must redefine. For instance, try assigning cell.userInteractionEnabled = YES to all other cases and see what the result is.

0
0

The problem is that you are not allowing for the possibility that the cell that was correctly showing the image gets reused later and the image view is still in there.

Here are two solutions:

  1. set the tag value of the image view when you create it, then when you setup the cells, include code to check for and remove the old imageView if necessary.

  2. assign different reuse identifiers to cells that need an image view and those that do not. Then make sure that you are only adding a new image view to cells when they are being created and not when they are being reused.

- (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]; 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([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.userInteractionEnabled = NO;

}

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

(Those last few lines fell out of the code box). That should do it.

6
  • Hi Dancreek, I have tried using Tag but it didn't work! Just tried using different identifier for the cell with (IndexPath.Section=0 and IndexPath.Row=0) but that didn't work either. Nov 4, 2011 at 14:23
  • The issue is when I scroll, as UITableView uses Lazy Loading, it reassigns the section=0 and row=0 to current visible cell and when that cell gets called on cellForRowAtIndexPath, it creates the image! I just need image in first cell of first section. For rest of sections, first cell will have userinteraction disabled. Because of index of cells getting reset whenever I scrolled, this issue is not going to be resolved unless I remove (indexPath.section == 0 && indexPath.row == 0) condition from the code and use something else! Nov 4, 2011 at 14:24
  • 1
    Indexes of cells do not change. Otherwise they'd be useless. The problem is that your first cell is getting reused.
    – Dancreek
    Nov 4, 2011 at 14:50
  • Oh Right. Thanks. Vote up for that! Nov 4, 2011 at 15:11
  • Just tested what you said. Used different identifier for first cell and added image only when it has created but still it gives weird behavior! Image was jumping from one cell to other during scroll up and down and first cell was empty after scrolling. Could you point out some example that uses the things you mentioned please. For now I have resolved issue using stavsh's given solution but still wondering if there is better way. Nov 4, 2011 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.