一豌鸡、tableview單一子視圖自適應嘿般,這是最基礎最簡單的;
1涯冠、【重點】在tableView創(chuàng)建的時候設置rowHeigh屬性炉奴,注意千萬不要實現行高的代理方法,否則無效蛇更!
_tableView.rowHeight = UITableViewAutomaticDimension;
_tableView.estimatedRowHeight = 100;
2瞻赶、添加到單元格上的子視圖必須設置與單元格contentView約束關聯,相對布局派任。這里推薦使用Masonry很好使砸逊!
如果出現問題,請回頭檢查一二步吨瞎!
ps:提到tableView痹兜,自然就有cell的處理,cell上子視圖添加的時候颤诀,有兩種方式
[cell addSubview:<#(nonnull UIView *)#>];
[cell.contentView addSubview:<#(nonnull UIView *)#>];
兩者看上去沒啥區(qū)別字旭,但是出現單元格滑動需求的時候,前者直接添加到cell上的子視圖不會移動崖叫,而后者添加到cell.contentView上的子視圖可以隨整體左右移動遗淳。
so推薦使用后者,[cell.contentView addSubview:<#(nonnull UIView *)#>];
但是cell設置背景色則與上反之心傀,
cell.backgroundColor = [UIColor redColor];
cell.contentView.backgroundColor = [UIColor redColor];
在單元格滑動時屈暗,cell.backgroundColor是設置了整個單元格的背景色,而后者不是脂男。
二养叛、tableview內嵌collectionview子視圖自適應,比如列表里嵌子標簽宰翅;
單個tableview可以設置單元格自適應高度弃甥,同理單個collectionview也可如此,但兩者嵌套在一起汁讼,得先計算出collectionview的高度淆攻,再去自適應tableview高度阔墩。
1、同上先設置tableview自適應屬性瓶珊;
2啸箫、tableviewCell不必復用,回調刷新tableview:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellID = [NSString stringWithFormat:@"%li",indexPath.row];
CHClassiListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
cell.indexPath = indexPath;
__weak __typeof(cell) wCell = cell;
__weak typeof(self)weakSelf = self;
cell.reloadBlock = ^{
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.tableView reloadRowsAtIndexPaths:@[wCell.indexPath] withRowAnimation:UITableViewRowAnimationNone];
});
};
cell.model = self.classiArr[indexPath.row];
[cell setClickTagBlock:^(NSString *tagName,NSString *tagId,BOOL isChild) {
CHTagCourseListVC *vc = [[CHTagCourseListVC alloc] init];
vc.tagName = tagName;
vc.tagId = tagId;
vc.isChild = isChild;
vc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:vc animated:YES];
}];
return cell;
}
3伞芹、重點處理UICollectionViewFlowLayout子標簽瀑布流忘苛!
@interface CHChildTagFlowLayout()
@property (nonatomic, assign) NSInteger row;
@property (nonatomic, assign) NSInteger maxWidth;
@property (nonatomic, assign) CGFloat maximumSpacing;//行距列距都一樣,就只定義了一個參數
@property (nonatomic, assign) CGFloat maxHeight;
@property(nonatomic,copy) void(^updateCollectViewHeightBlock)(CGFloat height);
@end
@implementation CHChildTagFlowLayout
- (CGFloat)maxHeight
{
if (!_maxHeight) {
_maxHeight = 20;
}
return _maxHeight;
}
- (void)updateCollectViewHeightBlock:(void(^)(CGFloat height))updateCollectViewHeightBlock
{
self.updateCollectViewHeightBlock = updateCollectViewHeightBlock;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSMutableArray * attributes = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
self.maximumSpacing = 16;
self.maxWidth = self.maximumSpacing;
self.row = 0;
[attributes enumerateObjectsUsingBlock:^(UICollectionViewLayoutAttributes *currentLayoutAttributes, NSUInteger idx, BOOL * _Nonnull stop) {
CHClassiModel *model = self.tagArr[idx];
CGFloat x, y, w, h;
w = [self titleWidthOrHeight:Font(12) isWidth:YES width:CGFLOAT_MAX height:44 str:model.name] + 5;//這個5是為了容計算誤差
if (w > (rect.size.width - self.maximumSpacing*2)) {
w = rect.size.width - self.maximumSpacing*2;
}
x = self.maxWidth;
h = 20;
if( (self.maxWidth + w) > rect.size.width) {
x = self.maximumSpacing;
self.row = self.row + 1;
self.maxWidth = w + self.maximumSpacing*2;
} else {
self.maxWidth = self.maxWidth + w + self.maximumSpacing;
}
y = self.row * (h + self.maximumSpacing);
currentLayoutAttributes.frame = CGRectMake(x, y, w, h);
}];
self.maxHeight = (self.row + 1) * (20 + self.maximumSpacing) - self.maximumSpacing;
if (self.updateCollectViewHeightBlock) {
self.updateCollectViewHeightBlock(self.maxHeight);
}
return attributes;
}
- (CGSize)collectionViewContentSize {
return CGSizeMake(self.collectionView.width, self.collectionView.height);
}
- (CGFloat)titleWidthOrHeight:(UIFont *)font isWidth:(BOOL)isWidth width:(CGFloat)width height:(CGFloat)height str:(NSString *)str
{
NSDictionary *attrs = @{NSFontAttributeName:font};
CGRect contentRect = [str boundingRectWithSize:CGSizeMake(width, height)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:attrs
context:nil];
if (isWidth) {
return contentRect.size.width;
}
return contentRect.size.height;
}