Im following the concept mentioned in https://stackoverflow.com/a/167129/127/... but in a slightly different way to implement a dynamic cell using auto layout. I have only one user cell prototype. But I have to add some UILabel elements to the cell based on the data array and maintain the dynamic height of the cell.
// 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++) {
[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];
[cell addLabel:anAry];
[cell layoutIfNeeded];
return cell;
}
if I do not use this didAddLabel check then scroll the table. If I use this, then only I get four cells with different heights.
" ..."
/ ?
.