更改所选UITableViewCell的背景颜色

时间:2015-08-09 19:10:58

标签: ios swift uitableview

点击时我试图改变单元格的背景。 它类似于css的悬停或焦点,我该怎么做?

   // 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 个答案:

答案 0 :(得分:3)

cellForRowAtIndexPath中添加此代码。感谢@danh,添加检查是否已经selectedBackroundView,因此无需分配新的。{/ p>

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

答案 1 :(得分:1)

为什么不只是将UITableViewCell子类化,并且在单元格的构造过程中(您在子类化单元格上的init方法),还可以向其中添加背景单元格?

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

        // set your background view here

        // add your constraints here
    } 
}

这样,您在调用cellForRowAtIndexPath时不会创建UIView,这可能会降低性能。然后,当您选择该行时,您只需抓住该单元格并对其进行任何操作即可。

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