sborlab
sborlab

Reputation: 21

Swift TableView pass selected Cell

I have two TablewViewControllers: ListTableViewController which holds my list of items and a SortTableViewController where I store sort options for the ListTableView. I need to find a way to highlight the active (if user clicked a sort option) sort option (Cell) when the SortTableViewController loads.Any ideas how to do this?

Screenshot

Upvotes: 0

Views: 68

Answers (2)

Wes
Wes

Reputation: 1042

If you need the sort selection to persist, you can try this. Use NSUserDefaults to store the selected row index (this assumes your sort options list is fixed)

    class SortTableViewController : UITableViewController {

        let selectedRowIdx = NSUserDefaults.standardUserDefaults().integerForKey("PLACE_YOUR_KEY_HERE")

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

            let cell = // your implementation here

            if indexPath.row == selectedRowIdx {
                cell.textLabel?.textColor = UIColor.redColor()
            }
        }

        override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

            NSUserDefaults.standardUserDefaults().setInteger(indexPath.row, forKey: "PLACE_YOUR_KEY_HERE")
        }
    }

Upvotes: 1

Luca Angeletti
Luca Angeletti

Reputation: 59536

You ca try this

class SortTableViewController: UITableViewController {

    var highlightedIndexPath: NSIndexPath? //TODO: populate this

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = //TODO: populate this
        cell.textLabel?.textColor = indexPath == highlightedIndexPath ? .redColor() : .blackColor()
        return cell
    }
}

Upvotes: 0

Related Questions