微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我有一个带有标题的 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 举报,一经查实,本站将立刻删除。