AutoLayout自動布局cell

聲明:本人約束黨, 約束內容不講解,直接看代碼部分

最近整理一下

**之前的關于 自動布局cell 的文章的地址
http://www.lxweimin.com/p/7ebd8b02deba **
試用于iOS8之前的

iOS8之后自動布局cell

1. TableView 打開自動高度功能
- (void)viewDidLoad 函數里邊
_tab.estimatedRowHeight=44; // 最好給一個高度的大約的平局值
_tab.rowHeight=UITableViewAutomaticDimension;
2. 自定義高度給關掉

6DA775D5-6716-4F9E-8DAC-2CD311A71821.png

效果

QQ20170919-170951.gif

上主要的代碼

//
//  ViewController.m
//  auto
//
//  Created by jie on 2017/9/19.
//  Copyright ? 2017年 jie. All rights reserved.
//

#import "ViewController.h"
#import "AutoTableViewCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *_dataSource;
    __weak IBOutlet UITableView *_tab;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataSource = [[NSMutableArray alloc] initWithCapacity:0];
    for(int i = 0 ; i < 10 ;i++){
        switch (i) {
            case 0:{
                NSString * s = @"濕噠噠干哈師大暨大撒就開喝愛神的箭等哈手機寬帶哈手機寬帶哈啊饑渴大紅燒雞塊";
                [_dataSource addObject:s];
            }
                break;
            case 1:{
                NSString * s = @"濕噠噠干哈師大暨大撒就開喝愛神的箭等哈手機寬帶哈手機寬帶哈啊饑渴大紅燒雞塊噠干哈師大暨大撒就開喝愛神的箭等哈手機寬帶哈手機寬帶哈啊饑渴大紅燒雞塊噠干哈師大暨大撒就開喝愛神的箭等哈手機寬帶哈手機寬帶哈啊饑渴大紅燒雞塊噠干哈師大暨大撒就開喝愛神的箭等哈手機寬帶哈手機寬帶哈啊饑渴大紅燒雞塊噠干哈師大暨大撒就開喝愛神的箭等哈手機寬帶哈手機寬帶哈啊饑渴大紅燒雞塊";
                [_dataSource addObject:s];
            }
                break;
            case 2:{
                NSString * s = @"濕噠噠干哈師大暨大撒就開喝愛神的箭等哈手機寬帶哈手機寬帶哈啊饑渴大紅燒雞塊噠干哈師大暨大撒就開";
                [_dataSource addObject:s];
            }
                break;
            case 3:{
                NSString * s = @"濕噠噠干哈師大暨大撒就開喝愛神的箭等哈手機寬帶哈手機寬帶哈啊饑渴大紅燒雞塊噠干哈師";
                [_dataSource addObject:s];
            }
                break;
            case 4:{
                NSString * s = @"濕噠噠干哈師大";
                [_dataSource addObject:s];
            }
                break;
            case 5:{
                NSString * s = @"濕噠噠干哈師大暨大撒就開喝愛神的箭等哈手機寬帶哈手機寬帶哈啊饑渴大紅燒雞塊噠干哈師大暨大撒就開喝大暨大撒就開喝愛神的箭等哈手機寬帶哈手機寬帶哈啊饑渴大紅燒雞塊噠干哈師大暨大撒就開喝愛神的箭等哈手機寬帶哈手機寬帶哈啊饑渴大紅燒雞塊噠干哈師大暨大撒就開喝愛神的箭等哈手機寬帶哈手機寬帶哈啊饑渴大紅燒雞塊噠干哈師大暨大撒就開喝愛神的箭等哈手機寬帶哈大暨大撒就開喝愛神的箭等哈手機寬帶哈手機寬帶哈啊饑渴大紅燒雞塊噠干哈師大暨大撒就開喝愛神的";
                [_dataSource addObject:s];
            }
                break;
            default:
                break;
        }
    }
    

    _tab.estimatedRowHeight=44;
    _tab.rowHeight=UITableViewAutomaticDimension;
    [_tab reloadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 100;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *strId = @"AutoTableViewCell";
    AutoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strId];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"AutoTableViewCell" owner:self options:nil]lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    for(int i = 0; i < 5 ; i++){
        NSInteger index = arc4random()%5;
        if(i == 0){
            cell.labe1.text = _dataSource[index];
        }else if (i == 1){
            cell.labe2.text = _dataSource[index];
        }else if (i == 2){
            cell.labe3.text = _dataSource[index];
        }else {
            cell.labe4.text = _dataSource[index];
        }
    }
    return cell ;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}
@end
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容