標籤:
1.代碼實現Cell高度自適應的方法
通過代碼來實現,需要計算每個控制項的高度,之後擷取一個cell的
總高度,比較常見的是通過lable的文本計算需要的高度。
CGSize labelsize = [@"asdassdas" sizeWithFont:font constrainedToSize:CGSizeMake(320,2000) lineBreakMode:NSLineBreakModeWordWrap];
這樣就可以計算展示需要的高度,cell裡面展示的時候可以在代理的方法內放回高度就行了。今天要實現的
是通過auto layout實現擷取展示需要的高度,實現的具體是使用了一個第三方庫(https://github.com/forkingdog/UITableView-FDTemplateLayoutCell),簡化了實現,使用這個
庫可以讓你專註的設定約束,下載後把UITableView+FDTemplateLayoutCell.h,UITableView+FDTemplateLayoutCell.m拖入項目就好,也可以通過pod 安裝。
2.實現效果
3.實現代碼
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [arrData count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ MyTableViewCell *cell = [self.tabview dequeueReusableCellWithIdentifier:@"CTF"]; cell.txt.text = [arrData objectAtIndex:indexPath.row]; cell.img.image=[UIImage imageNamed:@"a.jpg"]; return cell;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return [tableView fd_heightForCellWithIdentifier:@"CTF" configuration:^(MyTableViewCell *cell) { cell.txt.text = [arrData objectAtIndex:indexPath.row]; cell.img.image=[UIImage imageNamed:@"a.jpg"]; }];}
上傳比較麻煩, 需要Demo的可以留郵箱,我會及時發。
IOS XIB Cell自適應高度實現