I have a tableView with custom cell.
I also have a .swift-file for this custrom cell.
In this file I have a function which doesn't have a sender:AnyObject in entering parameters.
How can I call tableView.reloadData() from this function?
I have a tableView with custom cell.
I also have a .swift-file for this custrom cell.
In this file I have a function which doesn't have a sender:AnyObject in entering parameters.
How can I call tableView.reloadData() from this function?
try to create a delegate. (which I would assume you know about, if not take a look in apple documentation about the delegate and protocols)
So the idea i would suggest is to create a function that will be implemented in your UITableViewController (or a UIViewController conforming to UITableViewDelegate protocol)
Firstly try to add a protocol on top of your CustomCell.swift file.
protocol CustomCellUpdater: class { // the name of the protocol you can put any
func updateTableView()
}
then inside your CustomCell.swift:
weak var delegate: CustomCellUpdater?
func yourFunctionWhichDoesNotHaveASender () {
...
delegate?.updateTableView()
}
after that in your UITableViewController
(or equivalent)
func updateTableView() {
tableView.reloadData() // you do have an outlet of tableView I assume
}
Lastly make your UITableview
class conform to the CustomCellUpdater
protocol
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "yourIdentifier", for: indexPath) as! YourTableViewCell
cell.delegate = self
}
In theory it should work. Let me know if I am missing anything
You can use a delegate and protocol to do this.
Go to the cell class and above add this protocol:
protocol updateCustomCell: class {
func updateTableView()
}
Add this variable inside your cell class:
weak var delegate: updateCustomCell?
Go to the class you want to update or access a variable there and add this function there:
func updateTableView() {
/* write what you want here
simply what this means is you are trying to say if something happed
in the custom cell and you want to update something or even want to
access something from the current class inside your customCell class
use this function not the protocol function
*/
}
Don't forget to implement the protocol inside the class (Very important).
© 2022 - 2024 — McMap. All rights reserved.