2017-06-14 20 views
0

我有UICollectionView與細胞,包含UIScrollView和裏面,還有我使用此代碼給init細胞與數據定義UIView(經由XIB)用戶更新內容定義的UIView內部UICollectionViewCell

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

所有的工作 - 視圖可見,btnTempClick爲c alled,但fillData不起作用。它不會改變按鈕的內容,仍然有默認的文本「按鈕」。如何解決這個問題?

+0

它不應該是self.btnTemp.setTitle(「BTN溫度」,爲:。中性) – Joshua

+1

@Joshua你說得對,我是一個白癡:-) ...發佈爲asnwer,我會接受它 –

回答

1

只是爲了讓它官方從評論中刪除它並將其作爲答案。

您是否嘗試過使用self.btnTemp.setTitle("btn temp", for:.normal)

1

我認爲你可以forControlState的setTitle嘗試