Is Weak Self Needed for Table View Cell Button Closure

Retain Cycle in closure in CellView of TableView

OK. So I have solved this issue. The problem was not with this cell, acctually a manager class was setting a listener twice and releasing it only once. That was causing reatain cycle in my class.
Thanks for all answers on this question.

Cell isn't being deallocated after table view is dismissed, being referenced by closure's context

The problem is that you access the infocell inside it's callback. If you use variable inside own callback, you should mark it as a weak by adding it to the capture list.

In your code it should be like this:

   infocell.addPhotoTapAction = { [unowned self, weak infocell] _ in
...
}

Callback closure function not working in cell

This line is wrong.

addSubview(menuButton)

Instead:

contentView.addSubview(menuButton)

get tableview cell button to add a new tableview cell

In tableView(_: cellForRowAt:), you need to create the cell using MyCell's init(style:reuseIdentifier:) instead of dequeueReusableCell(withIdentifier:for:), i.e.

let cell = MyCell(style: .default, reuseIdentifier: "MyCell")

Next, in buttonTapCallback closure you need to add a new element to myArray and call reloadData() on myTableView, i.e.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = MyCell(style: .default, reuseIdentifier: "MyCell")
cell.buttonTapCallback = {[weak self] in
self?.myArray.append("NewCell-\(indexPath.row)")
self?.myTableView.reloadData()
}
return cell
}

Note: Use Swift types when working with Swift instead of the Objective-C types. Use [String] instead of NSArray in your code.

What is the correct way to reload a tableView from inside a nib?

The problem is this line:

let vc = FriendRequestViewController()

After that, vc is the wrong view controller — it is just some view controller living off emptily in thoughtspace, whereas the view controller you want is the already existing view controller that's already in the view controller hierarchy (the "interface").



Related Topics



Leave a reply



Submit