ios - 更新 UICollectionViewCell 中用户定义的 UIView 的内容

标签 ios uiview uiscrollview uicollectionview

我有 UICollectionView 和单元格,其中包含 UIScrollView 并且在其中,有一个用户定义的 UIView(通过 XIB)

我正在使用此代码用数据初始化单元格:

 func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {

        for v in cell.contentView.subviews {

            if let scroll: UIScrollView = v as? UIScrollView {
                scroll.contentOffset = CGPoint(x: 0, y: 0)

                for s in scroll.subviews {
                    if let content: DetailedView = s as? DetailedView {
                        content.fillData()
                    }
                }
            }
        } 
    }

在我的 DetailedView 中,我有:

@IBDesignable
class DetailedView: UIView {

    @IBOutlet weak var btnTemp: UIButton!

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupView()
    }

    // Performs the initial setup.
    private func setupView() {
        let view = viewFromNibForClass()
        view.frame = bounds

        // Auto-layout stuff.
        view.autoresizingMask = [
            UIViewAutoresizing.flexibleWidth,
            UIViewAutoresizing.flexibleHeight
        ]

        addSubview(view)
    }

    // Loads a XIB file into a view and returns this view.
    private func viewFromNibForClass() -> UIView {

        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil).first as! UIView

        return view
    }

    func fillData(){
        self.btnTemp.titleLabel?.text = "btn temp"

        print("fill data")

    }


    @IBAction func btnTempClick(_ sender: Any) {
        print("Click")
    }
}

一切正常 - View 可见,btnTempClick 被调用,但 fillData 不工作。它不会改变按钮的内容,仍然有默认文本“按钮”。如何解决这个问题?

最佳答案

只是为了让它正式从评论中删除并将其作为答案。

您是否尝试过使用 self.btnTemp.setTitle("btn temp", for:.normal)

关于ios - 更新 UICollectionViewCell 中用户定义的 UIView 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44539226/

相关文章:

iOS 蓝牙配件最大速度?

iOS UIView 渲染到纹理——保持质量

ios - Storyboard在不同时间显示多个 View

ios - 如何根据 UIScrollview 内的按钮框架大小水平滚动 UIScrollView

objective-c - 为什么我有可能的保留周期警告

javascript - 如何在 "ios contenteditable element"中格式化文本后恢复平滑滚动?

ios - 内部有 UIScrollView 的 UIView 的屏幕截图

iOS 8 - UIScrollView 不滚动

ios - swift iOS - 当移动速度 = 0 时......图像仍然产生,我该如何阻止它?

ios - 从 IOS 应用程序访问人类可读的 UIElement 标签