Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add subviews to UITableViewCell

I have trouble when I add subviews to UITableViewCell. It's working when the table size is below the iPhone size.

But when the size is bigger, it makes some horrible effect like this when I'm scrolling :

enter image description here

It's supposed to be like this :

enter image description here Then I think it comes from the cell reuse. Here is a sample of my code :

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

    static NSString *kCellIdentifier = @"UITableViewCellStyleSubtitle";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
    if (cell == nil) {
        //construct the cell
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                       reuseIdentifier:kCellIdentifier] autorelease]; 


        //clear the previuous content
        NSLog(@"Il y a %d subviews", [[[cell contentView] subviews] count]);
        //[[[cell contentView] subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)];
        NSLog(@"Il y a %d subviews", [[[cell contentView] subviews] count]);
        [[cell textLabel] setBackgroundColor:[UIColor clearColor]];
        [cell setSelectionStyle:UITableViewCellEditingStyleNone];
    }    

    switch (indexPath.row) {
        case 0:
            [cell addSubview:titleEvent];
            break;
        case 1:
            //load the owner logo
            [cell addSubview:logoAsso];
            break;
        case 2:
            //StartDate
            [cell addSubview:clockImage];
            break;
        case 3:
            //EndDate
            [cell addSubview:clockEndImage]; 
            break;
        case 4:
            //Address
            [cell addSubview:adress];
            break;
        case 5:
            //map
            [cell addSubview:map];
            break;
        case 6:
            //header
            [Graphism configureSeparationCell:cell];
            break;
        case 7:
            //descritpion
            [cell addSubview:descriptionEvent];
            break;
        default:
            break;
    }
    return cell;
}

The subviews are attributs of the class, and configured in the method viewDidLoad. If you can tell me what i'm doing wrong, that would be such a relief.

like image 937
Nielsou Hacken-Bergen Avatar asked Aug 24 '11 09:08

Nielsou Hacken-Bergen


2 Answers

Try this code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentifier"] autorelease];

    } else {
        UIView *subView = (UIView *)[cell.contentView viewWithTag:1];
        if ([subView superview]) {
             [subView removeFromSuperview];
        }
    }

    UIView *subView = [[UIView alloc] init];
    subView.tag = 1;
    [cell.contentView addSubview:subView];
    [subView release];

    return cell;
}
like image 74
Hitesh Avatar answered Oct 22 '22 04:10

Hitesh


switch (indexPath.row) {
    case 0:


        if (![cell.contentView viewWithTag:11]) {

            titleEvent.tag = 11;

            [cell.contentView addSubview:titleEvent];
        }


        break;
    case 1:
        if (![cell.contentView viewWithTag:11]) {

            logoAsso.tag = 11;

            [cell.contentView addSubview:logoAsso];
        }

like this do for all switch cases

like image 36
Vijay-Apple-Dev.blogspot.com Avatar answered Oct 22 '22 05:10

Vijay-Apple-Dev.blogspot.com