How to unselect a uitableview cell when the user returns to the view controller

I have a uitableview cell highlighted when a user selects it and then is pushed to a detail view. I would like the cell to be unhighlighted when they return to the table view view controller.

how would I go about accomplishing this?

I'm guessing [cell.textLabel setHighlighted:NO]; in viewWillAppear, but cell is undeclared if I put it there.

thanks for any help


If you using UITableViewController subclass then just set property

self.clearsSelectionOnViewWillAppear = YES;

else on viewDidAppear just call

NSIndexPath *indexPath = self.tableView.indexPathForSelectedRow;
if (indexPath) {
    [self.tableView deselectRowAtIndexPath:indexPath animated:animated];
}

// MARK: - Swift 3
if let indexPath = tableView.indexPathForSelectedRow {
    tableView.deselectRow(at: indexPath, animated: animated)
}

Swift 3.0

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    if let selectionIndexPath = self.tableView.indexPathForSelectedRow {
        self.tableView.deselectRow(at: selectionIndexPath, animated: animated)
    }
}
链接地址: http://www.djcxy.com/p/60018.html

上一篇: 在iOS5中单选时取消选择单元格?

下一篇: 如何在用户返回视图控制器时取消选择可用视图单元格