注意:
autoLayout 可以在- (void)viewDidAppear:(BOOL)animated 可以獲取到正確的frame。
- 自動(dòng)布局下Autolayout 一直是一個(gè)難點(diǎn)
- 自己前些天寫這個(gè)時(shí)候(雖然寫過好多次),被一個(gè)問題沒注意的小地方困擾好長時(shí)間,所以打算記錄下這點(diǎn),也算為自己提一個(gè)醒
- 話不多說,上圖,上代碼
- 本人比較懶,用的是IB拖拽的方式
- 非常記憶憂心的一點(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)
CGFloat height = [self.cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height+1;
為什么+1,他是cell分割線的高度 這個(gè)很重要
CGFloat preMaxWaith =[UIScreen mainScreen].bounds.size.width - 25;
preMaxWaith 他是明確自動(dòng)布局的的寬度
- 拖拽的布局約束有個(gè)非常重要的
Snip20160221_9.png
Snip20160221_10.png
這個(gè)距離上下的邊距要是不指定明確,可能出現(xiàn)cell的subview出現(xiàn)錯(cuò)位移動(dòng)的情況
- 不管怎么樣,這個(gè)自動(dòng)布局的確方便了大家的Ui的布局繪制
- systemLayoutSizeFittingSize (6.0之后的API) 的效率是有一定的問題由于*** UITableView是一次性計(jì)算完所有Cell的高度***,如果你有1000個(gè)cell的數(shù)量,建議不要使用這個(gè)API,使用其他方式,如果的遇到了會(huì)在更新。。??????????
- 當(dāng)然圖文混排用這個(gè)也沒有問題
<p>
The last thing 附上github代碼地址
https://codeload.github.com/json-zhao/Blog_AutolayoutCell/zip/master