AutoLayout自動(dòng)布局cell

注意:
autoLayout 可以在- (void)viewDidAppear:(BOOL)animated 可以獲取到正確的frame。

  1. 自動(dòng)布局下Autolayout 一直是一個(gè)難點(diǎn)
  2. 自己前些天寫這個(gè)時(shí)候(雖然寫過好多次),被一個(gè)問題沒注意的小地方困擾好長時(shí)間,所以打算記錄下這點(diǎn),也算為自己提一個(gè)醒
  3. 話不多說,上圖,上代碼
  4. 本人比較懶,用的是IB拖拽的方式
  5. 非常記憶憂心的一點(diǎn)當(dāng)我初次用自動(dòng)布局的方式計(jì)算cell的高度時(shí)候發(fā)現(xiàn)好多網(wǎng)上的domo都有這樣那樣的問題,沒有給關(guān)鍵點(diǎn)寫出來,今天閑來無聊打算寫一下這個(gè)問題,希望大家共勉,對(duì)與錯(cuò)希望大家教育
    <p>

Snip20160221_2.png



這里做個(gè)說明 明確寬度 這個(gè)寬度只對(duì)適合創(chuàng)建XIB的寬度起作用
Snip20160221_3.png


Snip20160221_5.png


Snip20160221_6.png


Snip20160221_7.png



<p>

下面是主要代碼

     #import "ViewController.h"
     #import "CustomTableViewCell.h"
     #import "CustomTwoTableViewCell.h"
     @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
     @property (weak, nonatomic) IBOutlet UITableView *_tableList;
     @property(nonatomic,retain)NSMutableArray *dataArray;
     @property(nonatomic,retain)CustomTableViewCell *cell;
     @property(nonatomic,retain)CustomTwoTableViewCell *twoCell;
     @end


     @interface Model : NSObject
     @property(nonatomic,retain)NSString *title;
     @property(nonatomic,retain)NSString *content;
     @property(nonatomic,retain)NSString *contentTwo;
     @end
     @implementation Model



     @end

     @implementation ViewController

     - (void)viewDidLoad {
         [super viewDidLoad];
        self.dataArray = [[NSMutableArray alloc] initWithCapacity:0];
         for (int i = 0; i < 10; i++) {
             Model *model = [[Model alloc] init];
             if (i%5 == 1) {
                 model.title = @"殺寇決等哈就坑阿達(dá)就開始打哈看得見哈殺就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾到幾哈接電話";
                 model.content = @"的交伙食費(fèi)就開始放暑假上放暑假的剛就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾時(shí)間各回各家時(shí)間看剛換時(shí)間供貨商看估計(jì)是司空見慣和世界觀海口市個(gè)機(jī)會(huì)可是供貨商髖關(guān)節(jié)還是個(gè)開獎(jiǎng)號(hào)工商局和顧客極光誰開個(gè)機(jī)是";
                 model.contentTwo = @"Test開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑";
                 [self.dataArray addObject:model];
             }else if (i%5 == 2){
                 model.title = @"殺寇決等哈就坑阿達(dá)就開始打哈就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾哈殺到幾哈接電話";
                 model.content = @"的交伙食費(fèi)就開始放暑假上放暑假的剛換時(shí)間就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾各家時(shí)間看剛換時(shí)間供貨商看估計(jì)是司空見慣和世界觀海口市個(gè)機(jī)會(huì)可是供貨商髖關(guān)節(jié)還是個(gè)開獎(jiǎng)號(hào)工商局和顧客極光誰開個(gè)機(jī)是";
                   model.contentTwo = @"Test開始放暑假上放始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑";
                 [self.dataArray addObject:model];
             }else if (i%5 == 3){
                 model.title = @"殺寇決等哈就坑阿達(dá)就開始打";
                 model.content = @"的交伙食費(fèi)就開始放暑假上放暑假的剛換時(shí)間各回各家時(shí)間看剛換時(shí)間供貨商看估計(jì)是司空見慣和世界觀海口市個(gè)機(jī)會(huì)可是供貨商髖關(guān)節(jié)還是個(gè)開獎(jiǎng)號(hào)工商局和顧客極光誰開個(gè)機(jī)是";
                 model.contentTwo = @"Test開始放暑假上放始放暑假上放暑開開始放暑假上放始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始開始放暑假上放始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始開始放暑假上放始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始開始放暑假上放始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始開始放暑假上放始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始始放暑假上放暑開始放暑假上放暑開始放暑假上開始放暑假上放始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始開始放暑假上放始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑";
                 [self.dataArray addObject:model];
             }else if (i%5 == 4){
                 model.title = @"殺寇決等哈就坑阿接電話";
                 model.content = @"的交伙食費(fèi)就開始";
                 model.contentTwo = @"Test開始放開始放暑假上放始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始開始放暑假上放始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始開始放暑假上放始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始開始放暑假上放始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始開始放暑假上放始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始開始放暑假上放始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始開始放暑假上放始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始開始放暑假上放始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始開始放暑假上放始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始開始放暑假上放始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始暑假上放始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑開始放暑假上放暑";
                 [self.dataArray addObject:model];
             }else{
                 model.title = @"殺寇決等哈就坑阿達(dá)就開始打哈看得見哈殺到幾哈接電話";
                 model.content = @"的交伙食費(fèi)就開始放暑就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾就坑阿達(dá)就開始打哈看得見哈殺到幾假上放暑假的剛換時(shí)間各回各家時(shí)間看剛換時(shí)間供貨商看估計(jì)是司空見慣和世界觀海口市個(gè)機(jī)會(huì)可是供貨商髖關(guān)節(jié)還是個(gè)開獎(jiǎng)號(hào)工商局和顧客極光誰開個(gè)機(jī)是";
                 model.contentTwo = @"Test開始放暑假上放始放暑";
                 [self.dataArray addObject:model];
             }
         }
         [self._tableList reloadData];

     }
     -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
     {
         return 2;
     }
     -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
     {
         return self.dataArray.count;
     }
     -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
         if (indexPath.section == 0) {
             static NSString *str =@"CustomTableViewCell";
             self.cell = [tableView dequeueReusableCellWithIdentifier:str];
             if (self.cell == nil) {
                 self.cell = [[[NSBundle mainBundle]loadNibNamed:@"CustomTableViewCell" owner:self options:nil]lastObject];
             }
             self.cell.selectionStyle = UITableViewCellSelectionStyleNone;
    Model *model = [self.dataArray objectAtIndex:indexPath.row];
             self.cell.titleLabel.text = model.title;
             self.cell.contentLabel.text = model.content;
             return self.cell;

         }else{
             static NSString *str =@"CustomTwoTableViewCell";
             self.twoCell = [tableView dequeueReusableCellWithIdentifier:str];
             if (self.twoCell == nil) {
                 self.twoCell = [[[NSBundle mainBundle]loadNibNamed:@"CustomTwoTableViewCell" owner:self options:nil]lastObject];
    }
             self.twoCell.selectionStyle = UITableViewCellSelectionStyleNone;
             Model *model = [self.dataArray objectAtIndex:indexPath.row];
             self.twoCell.title.text = model.title;
             self.twoCell.content.text = model.content;
             self.twoCell.twoContent.text = model.contentTwo;
             return self.twoCell;
         }         
     }
     -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
     {
         CGFloat preMaxWaith =[UIScreen mainScreen].bounds.size.width - 25;
         if (indexPath.section  == 0) {
             Model *model = [self.dataArray objectAtIndex:indexPath.row];
             self.cell.titleLabel.text = model.title;
             self.cell.contentLabel.text = model.content;
             [self.cell.titleLabel setPreferredMaxLayoutWidth:preMaxWaith];
             [self.cell.contentLabel setPreferredMaxLayoutWidth:preMaxWaith];
             [self.cell setNeedsUpdateConstraints];
             [self.cell updateConstraintsIfNeeded];
             CGFloat height = [self.cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height+1;
             return height;
         }else{
             Model *model = [self.dataArray objectAtIndex:indexPath.row];
             self.twoCell.title.text = model.title;
             self.twoCell.content.text = model.content;
             self.twoCell.twoContent.text = model.contentTwo;
             [self.twoCell.title setPreferredMaxLayoutWidth:preMaxWaith];
             [self.twoCell.content setPreferredMaxLayoutWidth:preMaxWaith];
             [self.twoCell.twoContent setPreferredMaxLayoutWidth:preMaxWaith];
             [self.twoCell setNeedsUpdateConstraints];
             [self.twoCell updateConstraintsIfNeeded];
             CGFloat height = [self.twoCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height+1;
             return height;
         }
     }
     @end

<p>

請(qǐng)看效果


Snip20160221_11.png

Snip20160221_12.png



<p>

說幾點(diǎn)注意事項(xiàng)

  1.              CGFloat height = [self.cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height+1;
    



為什么+1,他是cell分割線的高度 這個(gè)很重要

  1.          CGFloat preMaxWaith =[UIScreen mainScreen].bounds.size.width - 25;
    



preMaxWaith 他是明確自動(dòng)布局的的寬度

  1. 拖拽的布局約束有個(gè)非常重要的

    Snip20160221_9.png



    Snip20160221_10.png


    這個(gè)距離上下的邊距要是不指定明確,可能出現(xiàn)cell的subview出現(xiàn)錯(cuò)位移動(dòng)的情況

  2. 不管怎么樣,這個(gè)自動(dòng)布局的確方便了大家的Ui的布局繪制
  3. systemLayoutSizeFittingSize (6.0之后的API) 的效率是有一定的問題由于*** UITableView是一次性計(jì)算完所有Cell的高度***,如果你有1000個(gè)cell的數(shù)量,建議不要使用這個(gè)API,使用其他方式,如果的遇到了會(huì)在更新。。??????????
  4. 當(dāng)然圖文混排用這個(gè)也沒有問題
    <p>
    The last thing 附上github代碼地址
    https://codeload.github.com/json-zhao/Blog_AutolayoutCell/zip/master
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,825評(píng)論 6 546
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,814評(píng)論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,980評(píng)論 0 384
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 64,064評(píng)論 1 319
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,779評(píng)論 6 414
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,109評(píng)論 1 330
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,099評(píng)論 3 450
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 43,287評(píng)論 0 291
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,799評(píng)論 1 338
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,515評(píng)論 3 361
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,750評(píng)論 1 375
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,221評(píng)論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,933評(píng)論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,327評(píng)論 0 28
  • 開封第一講書人閱讀 36,667評(píng)論 1 296
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 52,492評(píng)論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,703評(píng)論 2 380

推薦閱讀更多精彩內(nèi)容