6

I’m following the concept mention in https://stackoverflow.com/a/18746930 but slightly in different way to implement the dynamic cell using auto layout. I have only one prototype custom cell. But I have to add multiple UILabel to the cell based on data array and have to maintain the dynamic cell height.

//My UITableViewCell

@interface CustomCell ()
@property (nonatomic, assign) BOOL didAddLabel;
@end
@implementation CustomCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

-(void)addLabel:(NSArray*)someAry{

    if(!didAddLabel){

        for (int i=0; i<someAry.count; i++) {

            // Initialize UILabel Programtically ...
            // adding label
            [self.aViewOfContaintView addSubview:aLabel];
        }


        self.heightLayoutConstraintOfaViewOfContaintView.constant = value;
        [self.aViewOfContaintView layoutIfNeeded];

        self.didAddLabel = YES;
    }
}
@end

//My UIViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    //.. .. ..
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 100;
    //.. .. ..
}

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


    CustomCell *cell = (CustomCell *)[theTableView dequeueReusableCellWithIdentifier:CellIdentifier];



    // Configure the cell for this indexPath
    //.. .. ..

    //Add Label
    [cell addLabel:anAry];

    [cell layoutIfNeeded];
    return cell;
}

if I don’t use that checking didAddLabel, then scrolling table sticking. If I use that then only I get four cells of distinct height.

the above answer mentions “For every unique set of constraints in the cell, use a unique cell reuse identifier. …”

How to use/register different reuse identifiers to same custom cell? Any alternative solution or any help will be appreciated.

Community
  • 1
  • 1
Mehedi Hasan
  • 329
  • 2
  • 7

1 Answers1

-1

You should use manual registration:

This method

You can use both classes / nibs and register same class / nib for multiple reuse identifiers. Your dequeue will still be the same

  • I have a tableview in MyViewController with CustomCell and idetifier is "CellID". The cell is wired to my CustomCell class. If I use something like this NSString *cellID = [NSString stringWithFormat:@"CellID%d", (int)someAry.count]; [self.tableView registerClass:[BookingCell class] forCellReuseIdentifier:cellID]; CustomCell *cell = (CustomCell *)[theTableView dequeueReusableCellWithIdentifier:cellID]; instead, then I get empty cell, may be cause of there aren't any cell in queue. Can you provide any sample code. – Mehedi Hasan Aug 29 '16 at 04:08