Can I call a function when UITableView has finished loading?

10,213

Solution 1

Try this delegate method, when this is called, the text is already in the cell, and you can test for it and add or remove the accessory. From the docs:

Discussion A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out.

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

// check text and do something
}

Solution 2

The UITableView only loads rows that will be visible on screen, this is a great feature as it does not need to generate all rows at once, which means lower memory usage and better performance. Because of this, there is no concept of a UITableView finishing loading because it never does. Imagine if you have 20 rows and only 8 are shown on screen, the user can scroll up or down causing new rows to be shown and old ones to be recycled. So as the others have said, the place to do this is when the cell is setup at cellForRowAtIndexPath.

Share:
10,213
gohar94
Author by

gohar94

Updated on June 04, 2022

Comments

  • gohar94
    gohar94 almost 2 years

    I want to change the accessory type of certain rows of my table depending on the text they contain. I thought one way to do that would be when the table has loaded, I can iterate over the table and check if each cell meets my condition. I don't know how to call a function when the UITableView has once finished loading.

  • gohar94
    gohar94 about 8 years
    Thanks a lot! Worked like a charm!