Martin Perry Martin Perry -4 years ago 49
iOS Question

Update content of user defined UIVIew inside UICollectionViewCell

I have

with cells, that contains
and inside it, there is a user defined
(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 {

In my DetailedView, I have:

class DetailedView: UIView {

@IBOutlet weak var btnTemp: UIButton!

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

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

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

// Auto-layout stuff.
view.autoresizingMask = [


// 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) {

All is working - view is visible,
is called, but
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