Martin Perry Martin Perry -4 years ago 49
iOS Question

Update content of user defined UIVIew inside UICollectionViewCell

I have

UICollectionView
with cells, that contains
UIScrollView
and inside it, there is a user defined
UIView
(via XIB)

I am using this code to init cell with data:

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


In my DetailedView, I have:

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


All is working - view is visible,
btnTempClick
is called, but
fillData
is not working. It does not change content of button, there is still default text "Button". How to fix this?

Answer Source

Just to make it official removing it from comment and putting it as an answer.

have you tried using self.btnTemp.setTitle("btn temp", for:.normal)?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download