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
Ruby: Loaderror - Library Not Found for Class Digest::Sha1 -- Digest/Sha1
Uibezierpath Subclass Initializer
How to Update Individual Array Element in Firebase with iOS Swift
Delete Image from Photo Gallery
iOS - Setobject VS. Synchronize
How to Update iOS 14 Widget Background Color from the App
How to Get Camera Calibration Data on iOS? Aka Avcameracalibrationdata
How to Add Followed Text to Uitextfield
Iad Interstitials Not Showing Consistently? and Not at All on the Simulator
Pure Swiftui Login, Signup, Register Flow, Is It Possible
Routing Between Points with Mapbox
Outline Uilabel Text in Uilabel Subclass
iOS Uiimagepickercontroller: Any Way of Getting the Date of the Chosen Picture
Displaying Instances of View Controllers Within Xcode Without Disturbing the Current Hierarchy