Reloading tableview data from custom cell
Asked Answered
P

2

10

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?

Pomfrey answered 31/8, 2015 at 18:20 Comment(1)
Are you truly looking for a cell to reload the entire table, or do you simply want to redraw the cell?Biota
M
28

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

Medullated answered 31/8, 2015 at 18:51 Comment(11)
how can I do "Lastly make your UITableview class conform to the CustomCellUpdater protocol"? :)Pomfrey
class YourUITableViewController: UITableViewController, CustomCellUpdater { ... ..... }Medullated
that's pity but nothing happenPomfrey
you right we forgot one thing. Where do you use customCell in your class?Medullated
in cellForRowAtIndexPath for examplePomfrey
didn't we forget anything else? :\Pomfrey
when you casting your cell ..... you do it like this right?: let cell = tableView.dequeueReusableCellWithIdentifier("YourCellIdentifier", forIndexPath:indexPath) as CustomCell \\ not the UITableViewCell and you did change the class of your cell in the storyBoard?Medullated
ofcourse, there is nothing problem except reloading tableView :)Pomfrey
put a println() inside updateTableView function to see if you are actually calling it.Medullated
that is all i got ... sorry :(Medullated
Man I was doing everything right, I just forgot to add cell.delegate = self and it caused me to troubleshoot for 2 hoursMezzo
O
1

You can use a delegate and protocol to do this.

  1. Go to the cell class and above add this protocol:

    protocol updateCustomCell: class { 
        func updateTableView() 
    }
    
  2. Add this variable inside your cell class:

    weak var delegate: updateCustomCell?
    
  3. 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).

Overmatch answered 15/8, 2018 at 18:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.