表格视图延迟(即使在删除手势识别器之后)

时间:2019-03-13 20:22:22

标签: swift uitableview tableview cell lag

当前,我有一个表格视图,该视图在有多个单元格时会滞后。当只有一个单元时,它运行平稳;但是,当填充多个单元格时,表视图的感觉很慢,有时会结结巴巴,并且不那么平滑。我删除了手势识别器,而是将其更改为didSelectRowAt,但是单元格中仍然存在滞后。这是单元格加载时的代码:

  public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if queryComplete == true {
        if displayConvo == true {
            let cell = tableView.dequeueReusableCell(withIdentifier: "messageCell", for: indexPath as IndexPath)  as! messageTableViewCell
            cell.profileImage.image = nil
            cell.selectionStyle = UITableViewCell.SelectionStyle.none

            var convoContent: messagesViewController.Convo
            convoContent = convoList[indexPath.row]

            convoContent.itemHeroID = "\(String(describing: convoContent.convoID))"
            cell.name.heroID = "\(String(describing: convoContent.convoID))"
            cell.lastMessage.text = convoContent.lastMessage
            cell.backgroundColor = UIColor.clear
            cell.postText.text = convoContent.postContent

            if convoContent.revealedBool == true {
                cell.profileImage.isHidden = false
                cell.profileImage.heroID = "\(String(describing: convoContent.convoID))"
                cell.name.text = convoContent.name
                cell.timer.isHidden = true
                if convoContent.profileImage == "nil" || convoContent.profileImage == nil {
                    let url =  URL(string:"https://firebasestorage.googleapis.comasdjfjaisfdji")
                    let processor = RoundCornerImageProcessor(cornerRadius: cell.profileImage.frame.size.width / 2)
                    cell.profileImage.kf.setImage(with: url, placeholder: nil, options: [.processor(processor)])
                }

                else {
                    let url = URL(string: convoContent.profileImage!)
                    let processor = RoundCornerImageProcessor(cornerRadius: cell.profileImage.frame.size.width / 2)
                    cell.profileImage.kf.setImage(with: url, placeholder: nil, options: [.processor(processor)])

                }
            }

            else {
                cell.profileImage.isHidden = true
                cell.timer.isHidden = false
                cell.timer.isUserInteractionEnabled = false
                cell.timer.heroID = "\(String(describing: convoContent.convoID))"
                let postDuration = convoContent.timeOfDeletion! - Int(convoContent.time!)
                let currentTime =  Date().timeIntervalSince1970 * 1000
                let difference = Int(round(Double(convoContent.timeOfDeletion! - Int(currentTime))))
                let date = Date(timeIntervalSinceNow: TimeInterval(difference / 1000))
                cell.name.text = String(timeToDelete(date: date as NSDate, numericDates: false))

                let amountOfCircleCovered = (Double(((convoContent.timeOfDeletion!) - Int(currentTime))) / Double(postDuration)) * 100
                var timerColor: UIColor?
                switch amountOfCircleCovered {
                case 0..<30:
                    timerColor = UIColor (red: 252/255, green: 110/255, blue: 81/255, alpha: 1)
                case 30..<60:
                    timerColor = UIColor (red: 255/255, green: 215/255, blue: 0/255, alpha: 1)
                case 60..<100:
                    timerColor =  UIColor(red: 26/255, green: 152/255, blue: 252/255, alpha: 1.0)
                default:
                    timerColor =  UIColor(red: 26/255, green: 152/255, blue: 252/255, alpha: 1.0)
                }
                print(amountOfCircleCovered)
                cell.timer.models = [   PieSliceModel(value: Double(100 - amountOfCircleCovered), color: UIColor(red: 220/255, green: 220/255, blue: 220/255, alpha: 1)),
                                        PieSliceModel(value: Double(amountOfCircleCovered), color: timerColor!),
                ]

            }
            let lastMessageDate = Date(timeIntervalSince1970: TimeInterval(convoContent.timeOfLastMessage! / 1000))
            cell.timeOfLastMessage.text = String(timeAgo(date: lastMessageDate as NSDate, numericDates: false))
            return cell

            }
        else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "messageCell", for: indexPath as IndexPath)  as! messageTableViewCell

            cell.lastMessage.text = "No new messages"
            return cell
            }
    }
    else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "messageCell", for: indexPath as IndexPath)  as! messageTableViewCell
        print ("loading")
        return cell
    }
}

此外,我还尝试过移动单元格的某些方面,以便每次将它们放在单元格文件中时都可以重置:

override func awakeFromNib() {
    super.awakeFromNib()

    profileImage.cornerRadius = profileImage.frame.size.width / 2
    profileImage.clipsToBounds = true
    profileImage.layer.borderColor = UIColor.white.cgColor
    profileImage.layer.borderWidth = 1

    timer.innerRadius = 0
    timer.outerRadius = timer.frame.width / 2
    timer.animDuration = 0.0
    timer.referenceAngle = 270
    timer.backgroundColor = UIColor.white
    postText.layer.zPosition = 1

    if !UIAccessibility.isReduceTransparencyEnabled {
        glossyView.backgroundColor = .clear

        let blurEffect = UIBlurEffect(style: .regular)
        let blurEffectView = UIVisualEffectView(effect: blurEffect)
        //always fill the view
        blurEffectView.frame = self.glossyView.bounds
        blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

        glossyView.addSubview(blurEffectView) //if you have more UIViews, use an insertSubview API to place it where needed
    } else {
        glossyView.backgroundColor = .black
    }

    timer.selectedOffset = 0
    glossyView.clipsToBounds = true
    glossyView.cornerRadius = 12.0
    glossyView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]

    messageBackground.clipsToBounds = true
    messageBackground.cornerRadius = 12.0
    messageBackground.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]
  //  glossyView.roundCornersView(corners: [.topLeft, .topRight], radius: 12.0)
   // messageBackground.roundCornersView(corners: [.bottomLeft,.bottomRight], radius: 12.0)
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
}

override func prepareForReuse() {
    profileImage.image = nil
    timer.isHidden = true
    timer.models = []

}

}

但是,当加载多个单元格时,表视图仍然滞后。我确保没有this link建议的手势识别器,并且还使用库来加载图像,所以我不确定为什么单元格仍然滞后。

1 个答案:

答案 0 :(得分:1)

本质上,UI大大降低了性能。我为部分填充的圆圈的png图像切换了饼图CA图层,并删除了所有Hero动画。结果,应用程序和表格视图都变得更加流畅。