问题
在我们的日常开发中,经常会使用到UICollectionViewCell、UITableViewCell的自适应。
解决方案
之前的MVC开发中,使用SnapKit 处理UICollectionViewCell 高度自适应的问题后,一直想抽时间整理一下,下面就用部分的代码,解释一下 我这边处理的思路。
Controller
//
// TaskVC.swift
// BossClient
//
// Created by qingxun on 2017/12/12.
// Copyright © 2017年 吕陈强. All rights reserved.
//
import UIKit
class TaskVC: BaseViewController {
fileprivate var dataArr:[TaskItemModel] = {
var data = [TaskItemModel]()
let model1 = TaskItemModel()
model1.name = "123"
model1.content = "123123123123123123123123123123123123123123123123123123123123123123";
data.append(model1);
let model2 = TaskItemModel()
model2.name = "5446"
model2.content = "3453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453451231345345345345345345345";
data.append(model2);
let model3 = TaskItemModel()
model3.name = "123"
model3.content = "123123123123123123123123123123123123123123123123123123123123123123";
data.append(model3);
let model4 = TaskItemModel()
model4.name = "5446"
model4.content = "3453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453453451231345345345345345345345";
data.append(model4);
let model5 = TaskItemModel()
model5.name = "qweqwe"
model5.content = "qweqweqwedasdqwewqeasdasdwqeqweasdasdqweqweasdasdweqweadasdqweqweasdasdqweqweasdadwqeqwasdasdadasdasdweqweqweqasdasdasdasdasdweqeqweqasd";
data.append(model5);
return data;
}()
//MARK:-滚动视图
lazy var collectionView:UICollectionView = { [unowned self] in
let flow = UICollectionViewFlowLayout();
//垂直滚动
flow.scrollDirection = .vertical
//最小行间距(当垂直布局时是行间距,当水平布局时可以理解为列间距)
flow.minimumLineSpacing = NEWHEIGHT(10);
//两个单元格之间的最小间距
flow.minimumInteritemSpacing = NEWWIDTH(10);
flow.estimatedItemSize = CGSize(width: kScreenW, height: NEWHEIGHT(150));
// flow.itemSize =
let collectionView = UICollectionView(frame: M_RECT(0, y: NEWHEIGHT(100), w: kScreenW, h: kScreenH - 64 - NEWHEIGHT(100)), collectionViewLayout: flow);
collectionView.delegate = self;
collectionView.dataSource = self;
collectionView.isScrollEnabled = true;