UITableView的优化

2016-02-25  本文已影响69人  王蓝胖

TableView 优化提示

优化常识

优化进阶

借助工具 - Instrument

缓存行高前.png

缓存行高

常量准备-在开发中要少用魔法数字

/// 工具栏高度
let HMStatusToolbarHeight: CGFloat = 35
/// 头像宽高
let HMStatusIconWH: CGFloat = 35
// 原创微博的 View
originalView.snp_makeConstraints { (make) -> Void in
    make.leading.equalTo(contentView)
    make.top.equalTo(contentView).offset(HMStatusCellMargin)
    make.trailing.equalTo(contentView)
}
// 底部 toolBar
statusToolBar.snp_makeConstraints { (make) -> Void in
    self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
    make.leading.equalTo(contentView)
    make.width.equalTo(contentView)
    make.height.equalTo(HMStatusToolbarHeight)
}
iconView.snp_makeConstraints { (make) -> Void in
    make.top.equalTo(self).offset(HMStatusCellMargin)
    make.leading.equalTo(self).offset(HMStatusCellMargin)
    make.size.equalTo(CGSizeMake(HMStatusIconWH, HMStatusIconWH))
}

计算行高

// MARK: - 计算行高
/// 计算行高
private func calcRowHeight() -> CGFloat {
    return 200;
}
/**
 1. originalView 高度
 顶部距离        HMStatusCellMargin
 顶部内容高度     HMStatusCellMargin + HMStatusIconWH + HMStatusCellMargin + 标签文本高度 + HMStatusCellMargin
 + (配图高度 + HMStatusCellMargin)
 2. retweetView 高度 HMStatusCellMargin + 标签文本高度 + + HMStatusCellMargin
 + (配图高度 + HMStatusCellMargin)
 3. toolBarView 高度
 */
/// 根据图片数量计算配图视图大小
private func calcSize(count: Int) -> CGSize {
    // 每一个条目之前的间距
    let itemMargin: CGFloat = 5
    // 每一个条目的宽高
    let itemWH: CGFloat = CGFloat(Int(SCREENW - itemMargin * 2 - HMStatusCellMargin * 2) / 3)
    
    // 计算当前显示的行数与列数
    let col = count == 4 ? 2 : (count >= 3 ? 3 : count)
    let row = count == 4 ? 2 : ((count - 1) / 3 + 1)
    
    // 宽度 = 列数*每一个条目的宽度 + (列数减1)*条目之前的间距
    let width = CGFloat(col) * itemWH + CGFloat(col - 1) * itemMargin
    let height = CGFloat(row) * itemWH + CGFloat(row - 1) * itemMargin
    
    return CGSize(width: width, height: height)
}
private func calcRowHeight() -> CGFloat {
    
    // 文本区域尺寸
    let textSize = CGSize(width: SCREENW - 2 * HMStatusCellMargin, height: CGFloat(MAXFLOAT))
    
    // 行高
    var height = HMStatusCellMargin + HMStatusToolbarHeight
    
    // 原创微博
    height += 3 * HMStatusCellMargin + HMStatusIconWH
    
    // 原创微博文字
    if let str = originalStatusAttr {
        height += str.boundingRectWithSize(textSize, options: [.UsesLineFragmentOrigin], context: nil).height
    }
    
    // 判断是否有转发微博
    if status?.retweeted_status != nil {
        height += 2 * HMStatusCellMargin
        
        if let str = retweetedStatusAttr {
            height += str.boundingRectWithSize(textSize, options: [.UsesLineFragmentOrigin], context: nil).height
        }
    }
    
    // 配图高度
    // 1> 配图数量,如果有转发微博配图,原创微博就一定没有配图
    let count = status?.retweeted_status?.pic_urls?.count ?? status?.pic_urls?.count
    
    // 2> 计算配图高度
    if count > 0 {
        height += HMStatusCellMargin
        
        height += calcSize(count!).height
    }
    
    return height;
}
/// 模型对应行高
var rowHeight: CGFloat = 0

...

// 构造函数

// 处理原创微博的内容 --> 生成一个带有表情的富文本
let result = dealStatusText(status.text)
originalStatusAttr = result.attr
originalLinkResults = result.linkResult

// 计算行高
rowHeight = calcRowHeight()

/// 返回缓存行高
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return statusListViewModel.statusList![indexPath.row].rowHeight
}

异步完成设置模型工作

// 通过 StatusDAL 加载`数据`
HMStatusDAL.loadStatus(sinceId, maxId: maxId) { (result) -> () in
    
    guard let statusDicts = result else {
        // 如果加载的数据不存在,直接返回
        complete(isSuccess: false, count: 0)
        return
    }
    
    dispatch_async(dispatch_get_global_queue(0, 0)) {
        
        var tempArray = [HMStatusViewModel]()
        
        // 遍历数组,转成模型
        for value in statusDicts {
            let status = HMStatus(dict: value)
            tempArray.append(HMStatusViewModel(status: status))
        }
        
        printLog("加载回来的数量\(tempArray.count)")
        if self.statusList == nil {
            self.statusList = [HMStatusViewModel]()
        }
        
        // 如果是上拉加载
        if isPullUp {
            // 拼接到数组的后面
            self.statusList = self.statusList! + tempArray
        } else {
            // 拼接到数组的前面
            self.statusList = tempArray + self.statusList!
        }
        
        dispatch_async(dispatch_get_main_queue()) {
            complete(isSuccess: true,count: tempArray.count)
        }
    }
}

减少配图视图尺寸计算

/// 配图视图大小
var pictureViewSize: CGSize = CGSizeZero
// 2> 计算配图高度
if count > 0 {
    height += HMStatusCellMargin
    
    pictureViewSize = calcSize(count!)
    
    height += pictureViewSize.height
}

修改配图视图 HMStatusPictureView

/// 设置图片 url 和 尺寸
///
/// - parameter urls: 配图 URL 数组
/// - parameter size: 配图视图尺寸
func setPicurls(urls: [HMStatusPictureInfo]?, viewSize: CGSize) {
    
    pic_urls = urls
    
    countLabel.text = "\(pic_urls?.count ?? 0)"
    
    let size = urls == nil ? CGSizeZero : viewSize
    
    self.snp_updateConstraints { (make) -> Void in
        self.sizeConstraint = make.size.equalTo(size).constraint
    }

    reloadData()
}
private var pic_urls: [HMStatusPictureInfo]?
// 设置数据
pictureView.setPicurls(pic_urls, viewSize: statusViewModel!.pictureViewSize)
// 设置数据
pictureView.setPicurls(pic_urls, viewSize: statusViewModel!.pictureViewSize)
上一篇 下一篇

猜你喜欢

热点阅读