4
votes

I was trying to change the background of cell when I clicked. It is similar to the hover or focus of css, how can I do that?

   // NO MOMENTO QUE SELECIONAR UM ITEM DO MENU
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {

        switch(indexPath.row)
        {

        case 0:
            var centerViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
            var centerNavController = UINavigationController(rootViewController: centerViewController)
            var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            appDelegate.centerContainer!.centerViewController = centerNavController
            appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
            break;

        case 1:
            var ensinoViewController = self.storyboard?.instantiateViewControllerWithIdentifier("EnsinoViewController") as! EnsinoViewController
            var ensinoNavController = UINavigationController(rootViewController: ensinoViewController)
            var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            appDelegate.centerContainer!.centerViewController = ensinoNavController
            appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
            break;
        case 2:
            var pesquisaViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PesquisaViewController") as! PesquisaViewController
            var pesquisaNavController = UINavigationController(rootViewController: pesquisaViewController)
            var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            appDelegate.centerContainer!.centerViewController = pesquisaNavController
            appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
            break;
        case 3:
            var extensaoViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ExtensaoViewController") as! ExtensaoViewController
            var extensaoNavController = UINavigationController(rootViewController: extensaoViewController)
            var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            appDelegate.centerContainer!.centerViewController = extensaoNavController
            appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
            break;

        case 4:
            var agendaViewController = self.storyboard?.instantiateViewControllerWithIdentifier("AgendaViewController") as! AgendaViewController
            var agendaNavController = UINavigationController(rootViewController: agendaViewController)
            var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            appDelegate.centerContainer!.centerViewController = agendaNavController
            appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
            break;
        case 5:
            var telefoneViewController = self.storyboard?.instantiateViewControllerWithIdentifier("TelefoneViewController") as! TelefoneViewController
            var telefoneNavController = UINavigationController(rootViewController: telefoneViewController)
            var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            appDelegate.centerContainer!.centerViewController = telefoneNavController
            appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
            break;

        default:

            println("\(menuItems[indexPath.row]) is selected");

        }
    }
}
2

2 Answers

4
votes

Add this code in cellForRowAtIndexPath. Thanks to @danh, added check if there's already selectedBackroundView so no need to allocate new one.

if cell.selectedBackgroundView == nil{
  cell.selectedBackroundView = UIView()
}
cell.selectedBackgroundView.backgroundColor = // your color
1
votes

Why not just subclass UITableViewCell and during the construction of the cell (your init method on the subclassed cell) you can add the background cell to it?

class CustomTableViewCell : UITableViewCell {
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        // set your background view here

        // add your constraints here
    } 
}

This way you aren't creating a UIView during your call to cellForRowAtIndexPath which can cost you in performance. Then when your row is selected you just grab the cell and do whatever you want with it.

let cell = tableView.cellForRow(at: indexPath)
cell.selectedBackgroundView.backgroundColor = // set your color here