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

问题描述 投票:0回答:1

目前,我有一个表视图,当有多个单元格时滞后。当只有一个小区时,它表现平稳;但是,当填充多个单元格时,表格视图的感觉很慢,有时会出现断断续续的情况,并且不够平滑。我删除了手势识别器,而是将其更改为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建议的手势识别器,并且图像正在使用库加载,所以我不确定为什么细胞仍然滞后。

swift uitableview tableview cell lag
1个回答
1
投票

从本质上讲,UI大大降低了性能。我为部分填充的圆圈的png图像切换了饼图时间CA图层,并删除了所有英雄动画。结果,app和tableview变得更加平滑。

© www.soinside.com 2019 - 2024. All rights reserved.