我遵循/sf/answers/1312285131/中提到的概念,但稍微以不同的方式使用自动布局实现动态单元格.我只有一个原型自定义单元格.但我必须根据数据阵列向单元格添加多个UILabel,并且必须保持动态单元格高度.
//我的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
Run Code Online (Sandbox Code Playgroud)
//我的UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
//.. .. ..
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 100;
//.. .. ..
}
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = …Run Code Online (Sandbox Code Playgroud)