是否可以缓存UI元素并忽略代码指令?

时间:2019-12-05 17:53:43

标签: ios swift uicollectionview uicollectionviewcell

我需要根据模型中的数据显示/隐藏 移动图标(按钮)

要向用户显示模型,我使用UICollectionView。单元格是一个自定义类,其中包含一个基于其创建的模型。

分配模型后,我立即检查其参数并调整单元格的外观。

我的案子:

如果我的一个单元格带有一个按钮,则删除它和模型。然后,我创建一个新模型和一个基于它的单元格,没有一个按钮。

尽管事实上,根据日志,所有条件都可以正常工作,并且有关模型和单元的信息正确显示-在屏幕上我看到了错误的显示。 我仍然看到不应该存在的按钮

我只是不知道可以与之建立联系,在这种情况下该怎么办?

我在下面附上我的日志,代码和屏幕截图。

class VideoCell: UICollectionViewCell {

    var model : VideoModel!{
        didSet {
            if let _ = model {

                if let _ = model.time {
                    timeLabel.text = model.time!.stringFromTimeInterval()
                } else {
                    timeLabel.text = NSLocalizedString("undefined", comment: "Время видео не определено")
                }

                Logger.Log("VideoCell model.didSet > model.id = \(model.id)")
                Logger.Log("VideoCell model.didSet > check storeLocal")

                if model.storeLocal {

                    Logger.Log("VideoCell model.didSet > storeLocal = \(model.storeLocal)")

                    self.model.thumbnail = UIImage(contentsOfFile: model.thumbLink)
                    storeButton.isHidden = false

                    Logger.Log("VideoCell model.didSet > storeButton.isHidden = false = \(storeButton.isHidden)")
                } else {
                    storeButton.isHidden = true

                    Logger.Log("VideoCell model.didSet > storeLocal = \(model.storeLocal)")
                    Logger.Log("VideoCell model.didSet > storeButton.isHidden = true = \(storeButton.isHidden)")
                }


                updateThumbnail()
            }
        }
    }

    func updateThumbnail(){
        if let image = self.model?.thumbnail {
            self.thumbnail.image = image
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    func configureCell() {
        self.thumbnail.image = nil
        self.thumbnail.contentMode = .scaleAspectFill

        let kHeight: CGFloat = self.frame.height / 500
        let kWidth: CGFloat = self.frame.width / 500

        Logger.Log("VideoCell cell.configureCell > initialize storeButton")

        storeButton = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        storeButton.backgroundColor = .clear
        storeButton.tintColor = Config.UIPreferences.Color2
        storeButton.setTitle("", for: .normal)
        storeButton.setImage(UIImage(named: "mobile"), for: .normal)
        storeButton.contentMode = .scaleAspectFit
        storeButton.contentEdgeInsets = UIEdgeInsets(top: 10, left: 5, bottom: 15, right: 20)
        storeButton.addTarget(self, action: #selector(storeButtonTapped), for: .touchUpInside)
        storeButton.isHidden = true
        self.addSubview(storeButton)

        Logger.Log("VideoCell cell.configureCell > storeButton.isHidden = \(storeButton.isHidden)")


        storeButton.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            storeButton.widthAnchor.constraint(equalToConstant: (130 * kWidth)+10),
            storeButton.heightAnchor.constraint(equalToConstant: (130 * kHeight)+10),
            storeButton.topAnchor.constraint(equalTo: self.topAnchor),
            storeButton.leadingAnchor.constraint(equalTo: self.leadingAnchor)
            ])
    }
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "VideoCell", for: indexPath) as? VideoCell{

            cell.configureCell()
            cell.delegate = controller
            cell.model = videos[indexPath.item]
            if let _ = cell.model {
                controller.getThumbImage(video: cell.model!)
            }

            Logger.Log("Day vc.cellForItemAt > cell.model.islocal = \(cell.model.storeLocal)")
            Logger.Log("Day vc.cellForItemAt > cell.model.id = \(cell.model.id)")

            return cell
        }

        return UICollectionViewCell()
    }

我的日志:

2019-12-05 23:48:47.842 *** VideoCell cell.configureCell > initialize storeButton ***  
2019-12-05 23:48:47.844 *** VideoCell cell.configureCell > storeButton.isHidden = true ***  
2019-12-05 23:48:47.849 *** VideoCell model.didSet > model.id = /var/mobile/Containers/Data/Application/228D4B45-9CBF-4EFF-9018-803871595521/Documents/localVideoStorage/05-12-2019/1575564524524.mp4 ***  
2019-12-05 23:48:47.851 *** VideoCell model.didSet > check storeLocal ***  
2019-12-05 23:48:47.855 *** VideoCell model.didSet > storeLocal = true ***  
2019-12-05 23:48:47.862 *** VideoCell model.didSet > storeButton.isHidden = false = false ***  
2019-12-05 23:48:47.865 *** Day vc.cellForItemAt > cell.model.islocal = true ***  
2019-12-05 23:48:47.868 *** Day vc.cellForItemAt > cell.model.id = /var/mobile/Containers/Data/Application/228D4B45-9CBF-4EFF-9018-803871595521/Documents/localVideoStorage/05-12-2019/1575564524524.mp4 ***  


2019-12-05 23:48:51.739 *** Day vc.updateVideo > before setting self.video ***  
2019-12-05 23:48:51.747 *** Day vc.numberOfItemsInSection > videos.count = 0 ***  


2019-12-05 23:49:09.940 *** Player router.dismissCamera() > call dayvc.refresh() ***  
2019-12-05 23:49:11.114 *** Day vc.updateVideo > before setting self.video ***  
2019-12-05 23:49:11.120 *** Day vc.numberOfItemsInSection > videos.count = 1 ***  
2019-12-05 23:49:11.123 *** VideoCell cell.configureCell > initialize storeButton ***  
2019-12-05 23:49:11.127 *** VideoCell cell.configureCell > storeButton.isHidden = true ***  
2019-12-05 23:49:11.130 *** VideoCell model.didSet > model.id = 1QctoIFxhwhNRxzvo7ImJhHYUMbuDHrqXJv ***  
2019-12-05 23:49:11.133 *** VideoCell model.didSet > check storeLocal ***  
2019-12-05 23:49:11.136 *** VideoCell model.didSet > storeLocal = false ***  
2019-12-05 23:49:11.139 *** VideoCell model.didSet > storeButton.isHidden = true = true ***  
2019-12-05 23:49:11.143 *** Day vc.cellForItemAt > cell.model.islocal = false ***  
2019-12-05 23:49:11.148 *** Day vc.cellForItemAt > cell.model.id = 1QctoIFxhwhNRxzvo7ImJhHYUMbuDHrqXJv ***

First video (with legal button) Second video (with illegal button)
第一个屏幕(带有合法的移动按​​钮)和第二个屏幕(带有非法的移动按​​钮)

0 个答案:

没有答案