【问题标题】:Update content of user defined UIView inside UICollectionViewCell更新 UICollectionViewCell 中用户定义的 UIView 的内容
【发布时间】:2017-06-14 08:17:10
【问题描述】:

我有带有单元格的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()
                    }
                }
            }
        } 
    }

在我的详细视图中,我有:

@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")
    }
}

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

【问题讨论】:

  • 不应该是self.btnTemp.setTitle("btn temp", for:.normal)
  • @Joshua 你是对的,我是个白痴 :-) ... 将其发布为 asnwer,我会接受它

标签: ios uiview uiscrollview uicollectionview


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我认为您可以尝试使用 setTitle forControlState

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      • 2021-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多