2

I created a custom cell for my table view in the side menu/navigation drawer and for the most part everything seems to be working fine, but when I select one of the rows (which takes me to another ViewController), then come back from the subsequent table view via the nav drawer, the cell is still gets diselected. For example: When i select A from the side menu table view i am taken to AViewController, when i select B i am taken to BViewController. So basically when i select A and come back to the side menu A should remain highlighted. How can i do that. Code :

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

    tableView.deselectRowAtIndexPath(indexPath, animated: true)
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! slideMenuIconTableViewCell

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    switch indexPath.row {
    case 0:
        let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("MyCartViewController")
        sideMenuViewController?.contentViewController = UINavigationController(rootViewController: viewController)
        sideMenuViewController?.hideMenuViewController()
        break
    case 1:
        let nextViewController:ProductListingViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ProductListingViewController") as! ProductListingViewController
        nextViewController.productID = "1"
        nextViewController.navigationTitle = "Tables"

        sideMenuViewController?.contentViewController = UINavigationController(rootViewController: nextViewController)
        sideMenuViewController?.hideMenuViewController()
        break
    case 2:
        let nextViewController:ProductListingViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ProductListingViewController") as! ProductListingViewController
        nextViewController.productID = "3"
        nextViewController.navigationTitle = "Sofas"

        sideMenuViewController?.contentViewController = UINavigationController(rootViewController: nextViewController)
        sideMenuViewController?.hideMenuViewController()
        break

    default:
        break
    }

Can anyone please help?

11
  • 4
    First of all, delete the break lines (except the break after default), a breakat the end of a case is not needed in Swift. Second of all, you're explicitly telling the table view to deselect the cell in the line tableView.deselectRowAtIndexPath.... Third of all: What is appDelegate for? It's not used in the code.
    – vadian
    Sep 7, 2016 at 10:24
  • PS: The cell isn't used either. You must not create reused cells outside cellForRowAtIndexPath anyway.
    – vadian
    Sep 7, 2016 at 10:34
  • @vadian Thanks, i will make the changes you suggested. But what do i use instead of tableView.deselectRowAtIndexPath Sep 7, 2016 at 10:59
  • If you want to keep the cell selected just delete the line.
    – vadian
    Sep 7, 2016 at 11:01
  • 1
    As it is not possible to check the whole code for the issue. What i guess is, you might be allocating the table or you might be reloading the table, possibly in viewWillAppear. You can check it by yourself and let us know if this is what your problem is
    – Janmenjaya
    Sep 7, 2016 at 12:36

3 Answers 3

2

You should not reload or allocate the table to keep the row's status as selected. As you are reloading the table in viewWillAppear, it resets the selection which results your table does not show previous selection. As your requirement is to reload it in viewWillAppear, Then the solution would be

1 - Keep a global variable to keep the object selected. (Keeping index may be erroneous as your list content is dynamic)

2 - Before reloading filter out the object previously assigned in the global object.

3 - Find out the index of object in the array.

4 - Then use the conditional statement inside the cellForRowAtIndex to make the particular row as highlighted.

Hope it will help you

Happy coding ...

1
  • I will be grateful if could please provide a code snippet of your explaination Sep 8, 2016 at 4:22
2

Guys i found the solution. I took help from what Janmenjaya suggested above (https://stackoverflow.com/a/39382360/6077720) and wrote the following code which worked :

var cellToDeSelect:UITableViewCell?
var isHighlighted: Bool?


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

    cellToDeSelect = tableView.cellForRowAtIndexPath(indexPath)!
    cellToDeSelect?.contentView.backgroundColor = UIColor.grayColor()
    isHighlighted = true
}

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

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! slideMenuIconTableViewCell
    cell.layoutMargins = UIEdgeInsetsZero
    cell.contentView.backgroundColor = UIColor.clearColor()
    if (isHighlighted == true){
        cellToDeSelect?.contentView.backgroundColor = UIColor.grayColor()

    }

    return cell
}
1

You can add a bool to the class slideMenuIconTableViewCell 'isHighlighted' and set this to true when selected. After you return to the tableview, check the boolean value and give the cell a other background color.

1
  • i am a little confused, could you please provide the code Sep 8, 2016 at 3:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.