gohar94
gohar94

Reputation: 138

Can I call a function when UITableView has finished loading?

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.

Upvotes: 3

Views: 11535

Answers (5)

Ashif Khan
Ashif Khan

Reputation: 9

May be, You can approach it like this,

func tableView(_tableView: UITableView,
               cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

    ///Cell Creation Code


    if indexPath.row == data.count{
        //call method asynchronously to indicate table finished loading
    }

    //return cell
}

But again it depends how many rows you have as data...

Upvotes: 0

Tommy
Tommy

Reputation: 989

You can call the method asyncrhonously.

inside your

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if indexPath.row == data.count-1{
    // Declare your cells here
    }

}

Upvotes: 0

koen
koen

Reputation: 5729

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
}

Upvotes: 8

totiDev
totiDev

Reputation: 5265

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.

Upvotes: 1

Harshal Bhavsar
Harshal Bhavsar

Reputation: 1673

You have to do the checking in cellForRowAtIndexPath and update the accessory view in the same function before returning cell.

As there is no such event for tableView Did finished loading

Upvotes: 0

Related Questions