如何优化UITableViewCell,因为我的UITableView滞后

时间:2017-10-20 14:27:53

标签: ios swift uitableview

好的,我的UITableView太复杂了,无法加载。当在UIPageViewController之间进行分页时,它会滞后,我无法简单快速地在页面之间滑动。有什么问题?

它是如何运作的?

我的UIViewController UITableView有很多单元格。这些视图控制器是UIPageViewController的一部分。在它们之间滑动......滞后。

如何优化应用以加快速度?

我发现这是因为我的UITableViewCell,当没有细胞时,它很简单......并且就像一个魅力。但是当细胞被加载时,它会滞后。

这是我的UITableViewCell

enter image description here enter image description here

这是我的cellForRow:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let address = fetchedResultsController.object(at: indexPath)
    let cell = tableView.dequeueReusableCell(withIdentifier: AddressTableViewCellIdentifier, for: indexPath) as! AddressTableViewCell

    cell.configure(with: address)
    cell.isSelectionMode = isSelectionMode
    cell.isSelected = selectedAddresses.contains(address)

    return cell
}

AddressTableViewCell

class AddressTableViewCell: UITableViewCell {

    @IBOutlet private var nameLabel: UILabel!
    @IBOutlet private var emptyButton: UIButton!
    @IBOutlet private var rejectedButton: UIButton!
    @IBOutlet private var dateForLastNoteLabel: UILabel!
    @IBOutlet private var addressNoteLabel: UILabel!
    @IBOutlet private var lastNoteLabel: UILabel!
    @IBOutlet private var returnVisitImageView: AttributedImageView!
    @IBOutlet private var regularDeliveryImageView: AttributedImageView!
    @IBOutlet private var studyImageView: AttributedImageView!
    @IBOutlet private var emailImageView: AttributedImageView!
    @IBOutlet private var phoneNumberImageView: AttributedImageView!
    @IBOutlet private var actionButtonsWidthConstraint: NSLayoutConstraint!

    @IBOutlet private var selectViewWidthConstraint: NSLayoutConstraint!
    @IBOutlet private var selectView: AttributedView!

    private var address: Address!

    var isSelectionMode: Bool = false {

        didSet {

            emptyButton.isEnabled = !isSelectionMode
            rejectedButton.isEnabled = !isSelectionMode
            selectViewWidthConstraint.constant = isSelectionMode ? 50 : 0
            accessoryType = isSelectionMode ? .none : .disclosureIndicator
        }
    }

    override var isSelected: Bool {

        didSet {
            selectView.backgroundColor = isSelected ? Settings.shared.currentMode.homeColor : UIColor.clear
        }
    }

    //MARK: - Class Methods

    //MARK: - Initialization

    //MARK: - Deinitialization

    //MARK: - Actions

    @IBAction func emptyButtonTapped(_ sender: UIButton) {
        save(withStatus: [0, 2].contains(address.status) ? 1 : 0)
    }

    @IBAction func rejectButtonTapped(_ sender: UIButton) {
        save(withStatus: [0, 1].contains(address.status) ? 2 : 0)
    }

    //MARK: - Open

    //MARK: - Internal

    func configure(with address: Address) {

        self.address = address

        let status = AddressStatus(rawValue: address.status)!

        addressNoteLabel.text = address.text
        nameLabel.text = address.name
        emptyButton.tintColor = status == .empty ? UIColor.black : UIColor.lightGray.withAlphaComponent(0.4)
        rejectedButton.tintColor = status == .rejected ? UIColor.red : UIColor.lightGray.withAlphaComponent(0.4)
        returnVisitImageView.isHidden = status != .returnVisit
        returnVisitImageView.topImageViewTintColor = Settings.shared.currentMode.homeColor
        regularDeliveryImageView.isHidden = !address.isRegularDelivery
        regularDeliveryImageView.topImageViewTintColor = Settings.shared.currentMode.homeColor
        studyImageView.isHidden = !address.isStudy
        studyImageView.topImageViewTintColor = Settings.shared.currentMode.homeColor
        emailImageView.isHidden = address.email == nil
        emailImageView.topImageViewTintColor = Settings.shared.currentMode.homeColor
        phoneNumberImageView.topImageViewTintColor = Settings.shared.currentMode.homeColor
        phoneNumberImageView.isHidden = address.phoneNumber == nil

        dateForLastNoteLabel.text = nil
        lastNoteLabel.text = nil
        actionButtonsWidthConstraint.constant = [AddressStatus.returnVisit, ].contains(status) || address.isStudy || address.isRegularDelivery ? 0 : 100

        if !returnVisitImageView.isHidden && address.lastNote == nil {
            dateForLastNoteLabel.text = "  "
        }

        if let note = address.lastNote {

            dateForLastNoteLabel.text = note.descriptiveDateAndTime
            lastNoteLabel.text = note.text
        }
    }

    //MARK: - Private

    private func save(withStatus status: Int16) {

        emptyButton.isEnabled = false
        rejectedButton.isEnabled = false

        MagicalRecord.save({ [weak self] context in

            self?.address.mr_(in: context)?.status = status

        }) { [weak self] _, error in

            guard let `self` = self else {
                return
            }

            UIAlertController.show(from: error)

            if error == nil {

                Sync.saveForSync(identifier: self.address.identifier, reason: 1)
                CloudAssistant.shared.performSyncing()
            }

            self.emptyButton.isEnabled = true
            self.rejectedButton.isEnabled = true
        }
    }

    //MARK: - Overridden
}

1 个答案:

答案 0 :(得分:0)

1)每个UIView和子类型在默认情况下都具有backgroundColor透明。尝试在每个视图(视图,按钮,图像,堆栈)上添加白色背景色。

2)在您的configure(带有地址:Address)上,检查您是否没有相同的地址。

3)尝试在约束设置上保持敏锐度,让Autolayout获得的布局越少,使用的cpu就越少。