简体   繁体   中英

UITableViewCell losses repeated content in other cell

I've a UITableView which when a cell has the same content that other, this content only appear in the las cell drawed. My custom cell adds an UIView property to add dynamic subviews from other class.

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    static NSString *MyIdentifier = @"cell";

    CollectionCell *cell = (CollectionCell *)[tableView 
                         dequeueReusableCellWithIdentifier:MyIdentifier];
    if (!cell) {
        cell = [[[CollectionCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                  reuseIdentifier:MyIdentifier] 
                autorelease];
    }

    [cell setCollectionView:/* Generated view in other class */];  

    return cell;
}

The concrete problem is:

My dynamic view is composed by, for example, 2 UILabels:

  • if label 1 is a title, the title is unique for each row -> No problem, renders fine.
  • if label 2 is a category, indexes from 0 to 5 have same category -> Only row at index 5 shows category label.

I can't create this labels in cell instantiation and add as subview because the cell content is all dynamic.

Thanks for your time and help.

UPDATE:

I can't create this labels in cell instantiation and add as subview because the cell content is all dynamic.

I'm going to explain it in detail:

The content and UI controls added to collectionView property can be differentes each execution. In one execution collectionView could have an UIImageView and a UILabel, and next execution it has 2 UILabels (for example). This is why I can't create something like this

    if (!cell) {
        cell = [[[CollectionCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                  reuseIdentifier:MyIdentifier] 
                autorelease];

        UILabel *foo = [[UILabel alloc] initWithFrame:SomeFrame];
        [foo setTag:101];
        [cell.collectionView addSubview:foo];
    }

    UILabel *foo = [cell.collectionView subviewWithTag:101];
    [foo setTitle:@"This content is dynamic"];

Thanks!

Update 2:

Appears to be a problem with custom UILabel subclass. If I use original UILabel to show strings works fine.

You are not supposed to add subviews outside the block-

if (!cell) {
        cell = [[[CollectionCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                  reuseIdentifier:MyIdentifier] 
                autorelease];
}

your subviews should only be added inside this block (the first time the reusable cell is created).

everything that happens outside (after) this 'if' block happens multiple times as you scroll your table up and down so that's where you edit the added subviews (only after the whole 'if block, outside it).

See my answer here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM