UITableView滞后于构建

时间:2018-08-04 12:07:43

标签: ios swift performance uitableview snapkit

我需要帮助调试一个性能问题。

概述

我有两个ViewController ABA点击B时按下UIButtonB包含UITableView

问题

当我在UIButton中单击A时,UI冻结1-2秒,然后出现B。当我从UITableView中删除B时,推送会顺利进行。

实施

建立单元格(我将标签留空):

addCustomSelectedBackgroundColor(.ma_tintLightBlue)
backgroundColor = .clear

// container view
let containerView = UIView()
containerView.backgroundColor = .ma_white
contentView.addSubview(containerView)
containerView.snp.makeConstraints {
    $0.edges.equalToSuperview().inset(UIEdgeInsets(top: 0.0, left: 0.0, bottom: 8.0, right: 0.0))
}

// avatar
authorAvatar = AvatarView(dimension: 46.0)
containerView.addSubview(authorAvatar)
authorAvatar.snp.makeConstraints {
    $0.leading.equalToSuperview().offset(16.0)
    $0.top.equalToSuperview().offset(10.0)
}

// date
dateLabel = UILabel()
dateLabel.font = .ma_systemFont(ofSize: 11.0)
dateLabel.textColor = .ma_textGray
containerView.addSubview(dateLabel)
dateLabel.snp.makeConstraints {
    $0.top.equalToSuperview().offset(14.0)
    $0.trailing.equalToSuperview().offset(-16.0)
}

// author info
let authorInfoView = buildAuthorInfoView()
containerView.addSubview(authorInfoView)
authorInfoView.snp.makeConstraints {
    $0.centerY.equalTo(authorAvatar.snp.centerY)
    $0.leading.equalTo(authorAvatar.snp.trailing).offset(12.0)
    $0.trailing.lessThanOrEqualTo(dateLabel.snp.leading).offset(-12.0)
}

// content
contentLabel = UILabel()
contentLabel.font = .ma_systemFont(ofSize: 14.0)
contentLabel.textColor = .ma_textBlack
contentLabel.numberOfLines = 0
containerView.addSubview(contentLabel)
contentLabel.snp.makeConstraints {
    $0.leading.equalToSuperview().offset(16.0)
    $0.trailing.equalToSuperview().offset(-16.0)
    $0.top.equalTo(authorAvatar.snp.bottom).offset(8.0)
}

// bottom bar
bottomView = PostCellBottomView()
containerView.addSubview(bottomView)
bottomView.snp.makeConstraints {
    $0.top.equalTo(contentLabel.snp.bottom).offset(20.0)
    $0.leading.equalToSuperview().offset(16.0)
    $0.trailing.equalToSuperview().offset(-16.0)
    $0.bottom.equalToSuperview()
}

建立表格视图

tableView = UITableView()
tableView.customize(cellClasses: [FeedWallCell.self])
view.addSubview(tableView)
tableView.snp.makeConstraints {
    $0.edges.equalToSuperview()
}

tableview的自定义方法

extension UITableView {

    func customize(cellClasses: [IdentifiableTableViewCell.Type] = [],
                   headerFooterClasses: [IdentifiableTableViewHeaderFooterView.Type] = [],
                   datasource: UITableViewDataSource? = nil,
                   delegate: UITableViewDelegate? = nil) {

        backgroundColor = .clear
        separatorStyle = .none
        keyboardDismissMode = .interactive

        estimatedRowHeight = 60
        rowHeight = UITableViewAutomaticDimension

        estimatedSectionHeaderHeight = 60
        sectionHeaderHeight = UITableViewAutomaticDimension

        estimatedSectionFooterHeight = 60
        sectionFooterHeight = UITableViewAutomaticDimension

        self.dataSource = datasource
        self.delegate = delegate

        register(cellClasses: cellClasses)
        register(headerFooterClasses: headerFooterClasses)
    }
}

分析

简单推送需要600毫秒以上的时间

enter image description here

环境

iOS11,Xcode9,iPhone6

0 个答案:

没有答案