从自定义标题单元格内的视图控制器更改按钮图像

问题描述

我有一个带有标题的 UICollectionView VC。此标题一个自定义单元格,在该单元格内有一个带有图像的按钮,我需要根据来自 API 或用户点击的信息更改该按钮。

在 func loadData 中,我获取有关图标的信息,然后在 headerCell 中调用函数来更改图标。我是否需要以其他方式执行此操作才能使其正常工作?

    protocol HeaderCellDelegate: class {
        func setDropdown()
        func tapOnSelection()
    }
    
    class HeaderCell: UIGestureRecognizerDelegate {
    
        // delegate
        weak var hDelegate: HeaderCellDelegate?
    
        @objc
        lazy var selectionBtn: UIButton = {
            let button = UIButton(type: .system)
            button.setTitle("One",for: .normal)
            button.addTarget(self,action: #selector(changeSelection),for: .touchUpInside)
            return button
        }()
        
        @objc
        lazy var oneOrTwo: UIButton = {
            let button = UIButton()
            button.setimage(UIImage(named: "oneOff")?.withRenderingMode(.alwaystemplate),action: #selector(toggleOneOrTwo),for: .touchUpInside)
            return button
        }()

        @objc
        lazy var iconBtn: UIButton = {
            let button = UIButton()
            button.setimage(UIImage(named: "iconGreen")?.withRenderingMode(.alwaystemplate),for: .normal)
             button.addTarget(self,action: #selector(setIcon),for: .touchUpInside)
            return button
            }()
        
        override init(frame: CGRect) {
            super.init(frame: frame)
        
            // I setup view helper
            // Just a button and icon on the right
            // button opens DropDown list
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        // MARK: - Delegate Functions
        
        @objc
        func toggleOneOrTwo(_ sender: Any) {
            self.hDelegate?.setDropdown()
        }
        
        @objc
        func changeSelection(sender: UIButton) {
            self.hDelegate?.tapOnSelection()
        }
        
        @objc
        func setIcon() {
            isIconGreen
                 ? iconColor.setimage(UIImage(named: "iconGreen")?.withRenderingMode(.alwaystemplate),for: .normal)
                 : iconColor.setimage(UIImage(named: "iconRed")?.withRenderingMode(.alwaystemplate),for: .normal)
        }

}

视图控制器

class ViewController: UICollectionViewController,UIGestureRecognizerDelegate {

  
    fileprivate let headerCellId = "headerCellId"
    lazy var headerCell = HeaderCell()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Register Cell Class
        self.collectionView.register(HeaderCell.self,forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,withReuseIdentifier: headerCellId)

        loadData()
    }

    override func collectionView(_ collectionView: UICollectionView,viewForSupplementaryElementOfKind kind: String,at indexPath: IndexPath) -> UICollectionReusableView {
        switch kind {
        case UICollectionView.elementKindSectionHeader:
            let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind,withReuseIdentifier: headerCellId,for: indexPath) as! HeaderCell
            header.backgroundColor = .groupTableViewBackground
            header.hDelegate = self
            
            return header
        default:
            fatalError("This should never happen!!")
        }
    }

    func loadData() {
         // get data from API and isIconGreen value
         let isIconGreen = true
         headerCell.setIcon(isIconGreen: isIconGreen)
    }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)