-
配图
字典数组
屏幕快照 2015-11-16 下午8.37.59.png
在status模型中加入配图属性
var pic_urls : [[string : string ]]?
-添加collectionView
构造函数的调用是底层自动转发的 init()->initWithFrame->initWithFrame:layout -
计算配图视图大小
首先拿到flowlayout再从flowlayout.item中获得itemSize设置大小private func calcViewSize() -> CGSize { // 1. 准备工作 let layout = collectionViewLayout as! UICollectionViewFlowLayout // 设置默认大小 layout.itemSize = CGSize(width: HMStatusPictureItemWidth, height: HMStatusPictureItemWidth) // 2. 根据图片数量来计算大小 let count = statusViewModel?.thumbnailURLs?.count ?? 0 // 1> 没有图 if count == 0 { return CGSizeZero } // 2> 1张图 if count == 1 { // TODO: - 临时返回一个大小 let size = CGSize(width: 150, height: 150) layout.itemSize = size return size } // 3> 4张图 2 * 2 if count == 4 { let w = 2 * HMStatusPictureItemWidth + HMStatusPictureItemMargin return CGSize(width: w, height: w) } // 4> 其他 /** 2, 3, 5, 6, 7, 8, 9 */ // 计算显示图片的行数 let row = CGFloat((count - 1) / Int(HMStatusPictureMaxCount) + 1) let h = row * HMStatusPictureItemWidth + (row - 1) * HMStatusCellMargin let w = HMStatusPictureMaxWidth return CGSize(width: w, height: h) }
Snip20150907_23.png
设置图片
-
计算行高
在statuscell中添加返回行高的方法/// - returns: 计算的行高 func rowHeight(viewModel: StatusViewModel) -> CGFloat { // 1. 设置视图模型 - 会调用 模型的 didSet statusViewModel = viewModel // 2. 有了内容之后更新约束 layoutIfNeeded() // 3. 返回底部视图的最大高度 return CGRectGetMaxY(bottomView.frame) }
在homeview控制器行高代理方法中设置行高
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
// 0. 获得模型
let viewModel = statusListViewModel.statuses[indexPath.row] as! StatusViewModel
// 1. 获得 cell
let cell = tableView.dequeueReusableCellWithIdentifier(HMStatusNormalCellID) as! StatusCell
// 2. 返回行高
return cell.rowHeight(viewModel)
}
行高代理方法介绍
-
缓存行高
在statusViewModel中定义行高
在homeViewController控制器 行高代理方法中记录行高
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {// 0. 获得模型 let viewModel = statusListViewModel.statuses[indexPath.row] as! StatusViewModel // 1. 判断视图模型的行高是否为0,如果不为0,表示行高已经缓存 if viewModel.rowHeight > 0 { printLog("返回缓存行高") return viewModel.rowHeight } // 2. 获得 cell,不能使用 indexPath 的方法,否则会出现死循环 let cell = tableView.dequeueReusableCellWithIdentifier(HMStatusForwardCellID) as! StatusForwardCell // 3. 记录行高 viewModel.rowHeight = cell.rowHeight(viewModel) return viewModel.rowHeight
}
-
转发模型
在status模型中定义转发属性
var retweeted_status : status ?
Snip20150907_44.png 转发界面布局
继承原创微博 添加、重写调度组演练
学了多线程 但是基本没用过 因为第三方框架都封装好了 加载网络用AFN 加载图片SDWebImage 都是在异步加载网络 主线程回调
调度组目的:监听一组异步任务完成