1.代碼實現Cell高度自適應的方法git
經過代碼來實現,須要計算每一個控件的高度,以後獲取一個cell的github
總高度,比較常見的是經過lable的文本計算須要的高度。spa
CGSize labelsize = [@"asdassdas" sizeWithFont:font constrainedToSize:CGSizeMake(320,2000) lineBreakMode:NSLineBreakModeWordWrap];
這樣就能夠計算展現須要的高度,cell裏面展現的時候能夠在代理的方法內放回高度就好了。今天要實現的代理
是經過auto layout實現獲取展現須要的高度,實現的具體是使用了一個第三方庫(https://github.com/forkingdog/UITableView-FDTemplateLayoutCell),簡化了實現,使用這個code
庫能夠讓你專一的設置約束,下載後把UITableView+FDTemplateLayoutCell.h,UITableView+FDTemplateLayoutCell.m拖入項目就好,也能夠經過pod 安裝。blog
2.實現效果it
3.實現代碼io
- (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的能夠留郵箱,我會及時發。table