iOS Autolayout 在tableView scrollView 適用 學(xué)習(xí)
1 如何自動(dòng)適應(yīng)cell的高度
autolayout 里面 使用 systemLayoutSizeFittingSize 方法 (系統(tǒng)通過 已知的完整的Constraints和view的屬性來計(jì)算高度)根據(jù)一個(gè)cell實(shí)例計(jì)算高度.
優(yōu)勢:不需要寫過多復(fù)雜的高度計(jì)算邏輯, 代碼簡潔. 強(qiáng)大
(1)首先依舊要在下面代理方法里實(shí)現(xiàn)讀取cell 高度 heightForRowAtIndexPath:
(2)計(jì)算高度 還是要考慮兩種情況
第一 如果不知道高度,計(jì)算一遍,存儲(chǔ)(盡量只計(jì)算一次,然后保存高度,計(jì)算需要布局 也是一個(gè)效率問題)
第二 知道高度 直接讀取
(3)關(guān)于cell的讀取
第一種 純代碼 寫法
這種 是我純代碼開發(fā)中經(jīng)常用到的
(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 為空 要?jiǎng)?chuàng)建新cell
static NSString *cellIdentifier = @"AHFHomeListCell"; AHFHomeListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (!cell) {//需要判空 ,如果為空 需要?jiǎng)?chuàng)建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:方法向數(shù)據(jù)源注冊cell(注意是Class 即 [xxxxxCell class])
//register cell:
static NSString *kCellIdentify = @"MyTableViewCell"; [self.tableView registerClass:[xxxxxCell class] forCellReuseIdentifier:kCellIdentify];
//cellForRowAtIndexPath:
//第一種實(shí)現(xiàn)
//dequeueReusableCellWithIdentifier:forIndexPath: 方法會(huì)直接調(diào)用注冊(所以必須有注冊方法),不需要判斷cell空值了就
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyTableViewCell" forIndexPath:indexPath];
//第二種實(shí)現(xiàn)
//dequeueReusableCellWithIdentifier:可以注冊 之后 不需要判空,可以不注冊需要判空如果為空 就創(chuàng)建新cell
static MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentify];
if (!cell) {
//如果使用 registerClass: 注冊過 則此處不需要處理 否則需要?jiǎng)?chuàng)建cell initWithStyle:reuseIdentifier
}
//TODO:填充UI數(shù)據(jù)
第三種 使用xib 加 注冊 cell
使用 tableView 的 registerNib:forCellReuseIdentifier:方法向數(shù)據(jù)源注冊cell
//register cell:
[self.tableView registerNib:[UINib nibWithNibName:@"MyTableViewCell" bundle:nil] forCellReuseIdentifier:kCellIdentify];
//cellForRowAtIndexPath:
實(shí)現(xiàn)規(guī)則同第二種方法里面的相應(yīng)代碼講解
關(guān)于兩種注冊: registerNib: 與 registerClass: 為什么可以不用去判空 ?
因?yàn)闊o可復(fù)用cell時(shí)runtime將使用注冊時(shí)提供的資源去新建一個(gè)cell并返回
2 如何在ScrollView中使用Autolayout (這里用Masonry 純代碼實(shí) CGFloat scrollWidth = HorizontalFrom750(225 + 20);
CGFloat scrollHeight =VerticalFrom750(290); pageWidth = scrollWidth;
//設(shè)置scrollView 約束 [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(self); make.width.equalTo(@(scrollWidth)); make.height.equalTo(@(scrollHeight)); }];
//使用containView 作為容器View 在容器View里面塞入目標(biāo)滾動(dòng)的子對象 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]; // 目標(biāo)滾動(dòng)元素 一個(gè)一個(gè) 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做動(dòng)畫
//TODO:
4 Autolayout在IOS6上的坑
//TODO:
參考:
https://blog.cnbluebox.com/blog/2015/02/02/autolayout2/
http://blog.csdn.net/youngsblog/article/details/44536143
posted on 2017-05-03 11:05 ACM_Someone like you 閱讀(312) 評論(0) 收藏 舉報(bào)
浙公網(wǎng)安備 33010602011771號