1 如何自動適應cell的高度ide
autolayout 裏面 使用 systemLayoutSizeFittingSize 方法 (系統經過 已知的完整的Constraints和view的屬性來計算高度)根據一個cell實例計算高度. 佈局
優點:不須要寫過多複雜的高度計算邏輯, 代碼簡潔. 強大動畫
(1)首先依舊要在下面代理方法裏實現讀取cell 高度 heightForRowAtIndexPath:url
(2)計算高度 仍是要考慮兩種狀況spa
第一 若是不知道高度,計算一遍,存儲(儘可能只計算一次,而後保存高度,計算須要佈局 也是一個效率問題).net
第二 知道高度 直接讀取代理
(3)關於cell的讀取code
第一種 純代碼 寫法對象
這種 是我純代碼開發中常常用到的blog
(1)cell 初始化要override 方法
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // TODO:cell頁面佈局 } return self; }
(2) dequeue 獲取cell 爲空 要建立新cell
static NSString *cellIdentifier = @"AHFHomeListCell"; AHFHomeListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (!cell) {//須要判空 ,若是爲空 須要建立cell initWithStyle:reuseIdentifier cell = [[AHFHomeListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; [cell.topicView setHandleSeletedOptionBlock:^(TopicOptions *options, TopicListModel *listModel,BOOL isOnlyRead) { NSString *optionId = isOnlyRead ? listModel.voted_option_id : options.option_id; [self openArticleVCArticleId:listModel.article_id img:listModel.banner andOptionId:[optionId integerValue] andIsRead:isOnlyRead]; }]; }
第二種 代碼 加 註冊cell
爲tableView註冊cell,使用registerClass:forCellReuseIdentifier:方法向數據源註冊cell(注意是Class 即 [xxxxxCell class])
//register cell:
static NSString *kCellIdentify = @"MyTableViewCell"; [self.tableView registerClass:[xxxxxCell class] forCellReuseIdentifier:kCellIdentify];
//cellForRowAtIndexPath:
//第一種實現
//dequeueReusableCellWithIdentifier:forIndexPath: 方法會直接調用註冊(因此必須有註冊方法),不須要判斷cell空值了就
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyTableViewCell" forIndexPath:indexPath];
//第二種實現
//dequeueReusableCellWithIdentifier:能夠註冊 以後 不須要判空,能夠不註冊須要判空若是爲空 就建立新cell
static MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentify];
if (!cell) {
//若是使用 registerClass: 註冊過 則此處不須要處理 不然須要建立cell initWithStyle:reuseIdentifier
}
//TODO:填充UI數據
第三種 使用xib 加 註冊 cell
使用 tableView 的 registerNib:forCellReuseIdentifier:方法向數據源註冊cell
//register cell:
[self.tableView registerNib:[UINib nibWithNibName:@"MyTableViewCell" bundle:nil] forCellReuseIdentifier:kCellIdentify];
//cellForRowAtIndexPath:
實現規則同第二種方法裏面的相應代碼講解
關於兩種註冊: registerNib: 與 registerClass: 爲何能夠不用去判空 ?
由於無可複用cell時runtime將使用註冊時提供的資源去新建一個cell並返回
2 如何在ScrollView中使用Autolayout (這裏用Masonry 純代碼實 CGFloat scrollWidth = HorizontalFrom750(225 + 20);
CGFloat scrollHeight =VerticalFrom750(290); pageWidth = scrollWidth;
//設置scrollView 約束 [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(self); make.width.equalTo(@(scrollWidth)); make.height.equalTo(@(scrollHeight)); }];
//使用containView 做爲容器View 在容器View裏面塞入目標滾動的子對象 UIView *containView = UIView.new; [self.scrollView addSubview:containView]; [containView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.scrollView); make.height.equalTo(self.scrollView); }];
self.cardViewArray = [NSMutableArray array]; self.cardButtonArray = [NSMutableArray array]; // 目標滾動元素 一個一個 add 在 conttainView上 注意邊界問題 UIView *lastView; CGFloat leftPadding = 0; //邊界 for (int i = 0; i < pageCount; i ++) { UIView *backView = UIView.new; [containView addSubview:backView]; [backView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(containView); make.width.equalTo(@(scrollWidth)); make.height.equalTo(self.scrollView.mas_height); if (lastView) { make.left.equalTo(lastView.mas_right); } else { make.left.equalTo(@(leftPadding)); } }]; lastView = backView; MethodMediaModel *music = self.musicArray[i]; MusicCardView *cardView = [[MusicCardView alloc]initWithFrame:CGRectZero]; [cardView setCardTitle:music.title imgUrl:music.img_url]; [cardView.playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside]; cardView.playButton.tag = i; [cardView.playButton setObj:music]; [backView addSubview:cardView]; [cardView mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(backView); make.left.equalTo(backView).offset(10); make.right.equalTo(backView).offset(- 10); make.height.equalTo(backView); }]; [self.cardViewArray addObject:backView]; [self.cardButtonArray addObject:cardView.playButton]; } [lastView mas_updateConstraints:^(MASConstraintMaker *make) { make.right.equalTo(containView.mas_right);//邊界
}];
3 使用Autolayout作動畫
//TODO:
4 Autolayout在IOS6上的坑
//TODO:
參考:
https://blog.cnbluebox.com/blog/2015/02/02/autolayout2/
http://blog.csdn.net/youngsblog/article/details/44536143