IOS XIB Cell自適應高度實現


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的可以留郵箱,我會及時發。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM