0

I need to run code which relies on the table view and all its cell's being loaded. The code needs to be run on the cells, but after they're initialised completely, not in cellForRowAtIndexPath (they need to have been drawn already because I'm using autolayout and need to run this code after it)

How can I know when they're loaded?

EDIT - i think theres some confusion

Basically every time i add a cell, cellForRowAtIndexPath creates this cell. It is not yet rendered. After this method, the cell is rendered. After render, i want to run some code

Hamzah Malik
  • 2,540
  • 3
  • 28
  • 46

3 Answers3

0

This is what worked. This runs after every cell has been drawn

override func viewDidLayoutSubviews() {
    for i in 0...3{
        let cell = table.cellForRowAtIndexPath(NSIndexPath(forRow: i, inSection: 0))
        //do what i needed
    }
}

Edit

This does not run after a cell is added, only works on cells which were there when the original table was loaded. What works better is putting this same method in the UITableViewCell class. That way it runs after each cell has been rendered:

override func viewDidLayoutSubviews() {
        let cell = self
        //do what i needed
}
Hamzah Malik
  • 2,540
  • 3
  • 28
  • 46
0

Use this method UITableView Delegate in your case:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath){
//Your required code will be here. 

}

This method will call when cell is rendered and you are viewing it.

Aadil Ali
  • 351
  • 1
  • 15
0
override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        for index in 0 ..< cell_Total_Count {
            if let cell = TableView.cellForRow(at: IndexPath(row: index, section: 0)) as? UITableViewCell_FileName {
               //Do cell stuff here...
          }
      }
 }
Vivek K
  • 1
  • 1
  • You can use viewWillLayoutSubviews() method because it gets called after rendering cell. – Vivek K Mar 26 '18 at 07:15
  • 4
    Answer should be always with some explanation, only code will be treated as **Low Quality Answer**. – Vishal Chhodwani Mar 26 '18 at 07:22
  • 2
    Although your code snippet might solve the issue, you should describe what’s the purpose of your code (how it solves the problem). Furthermore, you might want to check https://stackoverflow.com/help/how-to-answer – Ahmad F Mar 26 '18 at 07:45
  • While this code may answer the question, providing additional context regarding **how** and **why** it solves the problem would improve the answer's long-term value. – Alexander Mar 26 '18 at 10:25