user3672536
user3672536

Reputation: 61

Add "activity indicator" to TableView and restore to "disclosure indicator" in Swift

I have a TableView with each cells' accessory set to "disclosure indicator" which worked correctly.

Then, I decided to add an activity indicator while updating each cell with data from external sources. To accomplish this, I set:

cell.accessoryView = UIActivityIndicatorView(activityIndicatorStyle: .Gray)

When I'm done loading data from external sources, I've lost my "disclosure indicator" (the arrow on the right side of the cell). What should I restore "cell.accessoryView" to?

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

    let item = items[indexPath.row]
    var cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell

    // SET ACTIVITY INDICATOR HERE
    if cell.accessoryView == nil {
        let indicator = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
        cell.accessoryView = indicator
    }

    let indicator = cell.accessoryView as! UIActivityIndicatorView

    // ... some code here ...

    switch (item.state) {

        case .New:
            indicator.startAnimating()
            self.fetchExternalData(item, indexPath:indexPath)

        case .Downloaded:
            indicator.stopAnimating()

            // I'D LIKE TO RESTORE THE DISCLOSURE INDICATOR HERE
            cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
            // cell.accessoryView = ???
    }

    return cell
}

Upvotes: 3

Views: 2373

Answers (2)

famfamfam
famfamfam

Reputation: 536

of course you need to set

cell.accessoryType = UITableViewCellAccessoryNone;

before change accessoryView

Upvotes: 0

user3672536
user3672536

Reputation: 61

Oops, I just tried setting "cell.accessoryView = nil" and it worked.. I should have thought of that before posting :-(

Upvotes: 3

Related Questions