IOS XIB Cell自適應高度實現

來源:互聯網
上載者:User

標籤:

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自適應高度實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.