自定义UI TableViewCell选择了backgroundcolor swift

Pea*_*der 15 selected uitableview swift

我试图使用Swift更改自定义选定的TableViewCell的外观.

我需要通过设计师还是以编程方式完成?

我尝试了以下方法:

在此输入图像描述

这是我的代码:

@IBOutlet var tableView: UITableView!


    var tableData: [String] = ["One", "Two", "Three", "Four"]

    override func viewDidLoad() {
        super.viewDidLoad()


        // Register custom cell
        var nib = UINib(nibName: "vwTblCell", bundle: nil)
        tableView.registerNib(nib, forCellReuseIdentifier: "cell")

    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.tableData.count
    }

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

        var cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as TblCell

        cell.lblCarName.text = tableData[indexPath.row]
        cell.imgCarName.image = UIImage(named: tableData[indexPath.row])

        return cell

    }

    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
        println("Row \(indexPath.row) selected")
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 70
    }
Run Code Online (Sandbox Code Playgroud)

Cao*_*ong 21

我有一个相似的问题.在你的cellForRowAtIndexPath方法集中:

cell.selectionStyle = .None
Run Code Online (Sandbox Code Playgroud)

然后设置didHighlightRowAtIndexPath ...

func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) {
    let cell  = tableView.cellForRowAtIndexPath(indexPath)
    cell!.contentView.backgroundColor = .redColor()
}

func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath) {
        let cell  = tableView.cellForRowAtIndexPath(indexPath)
        cell!.contentView.backgroundColor = .clearColor()
}
Run Code Online (Sandbox Code Playgroud)


Chr*_*örz 14

你已经有了正确的方法:didSelectRowAtIndexPath.在那种方法中,您可以调用tableView.cellForRowAtIndexPath(indexPath)并获取您的单元格.您可以将单元格背景设置为您的颜色:

 func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
        println("Row \(indexPath.row) selected")
        let cell:YourCustomCell = tableView.cellForRowAtIndexPath(indexPath) as YourCell
        cell.backgroundColor = UIColor.redColor()
    }
Run Code Online (Sandbox Code Playgroud)

或者,更好的方法是检查您的cellForRowAtIndexPath方法,如果选择了一个单元格:

if(cell.selected){
  cell.backgroundColor = UIColor.redColor()
}else{
  cell.backgroundColor = UIColor.clearColor()
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢:)这是正确的方向。但是,我有一个问题:单击一个单元格时,它变成了原来的灰色选择颜色,当我单击下一个单元格时,先前选择的单元格的确变成了红色。但是当前选择的单元格始终为灰色。 (2认同)
  • 补充:"var myBackView = UIView(frame:cell.frame); cell.selectedBackgroundView = myBackView;" 你的代码.现在它有效. (2认同)

小智 14

我的两分钱:正确的做法(也是可视的)是在(tableView)单元格中使用指定的视图,即selectedBackgroundView属性.但是,您需要首先使用UIView()初始化它

SWIFT 3.0

override func awakeFromNib() {
    super.awakeFromNib()
    self.selectedBackgroundView = UIView()
    self.selectionStyle = .default // you can also take this line out
}
Run Code Online (Sandbox Code Playgroud)

然后您可以在自定义单元格中使用它,如下所示:

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    self.selectedBackgroundView!.backgroundColor = selected ? .red : nil
}
Run Code Online (Sandbox Code Playgroud)

而已.当然,您也可以将上面的内容集成到上面提到的UITableView函数中.看看这个.

  • 虽然这可能行得通,但这并不是正确的方法-仅在选择单元格时才会显示selectedBackgroundView,因此无需覆盖setSelected方法,只需在其中设置selectedBackgroundView?backgroundColor = .red `awakeFromNib()` (3认同)

Ibr*_*him 13

Swift 3的更新

这个答案是基于曹勇的回答,它的目的是作为Swift 3的更新

对于Swift 3,请在cellForRowAt indexPath方法集中使用以下代码:

cell.selectionStyle = .none
Run Code Online (Sandbox Code Playgroud)

然后,在didHighlightRowAtIndexPath中设置它

func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
    let cell  = tableView.cellForRow(at: indexPath)
    cell!.contentView.backgroundColor = .red
}

func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) {
    let cell  = tableView.cellForRow(at: indexPath)
    cell!.contentView.backgroundColor = .clear
}
Run Code Online (Sandbox Code Playgroud)

  • 不需要这个-只需设置单元格的“ selectedBackgroundView” (2认同)

Eli*_*tle 7

当您点击一个单元格时,实际上会更改子视图的背景色。该子视图为“ selectedBackgroundView”。您可以在cellForRowAtIndexPath TableView委托方法中覆盖每个单元格的视图。

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

   let cell = tableView.dequeueReusableCellWithIdentifier("identifier", forIndexPath: indexPath) 

   let selectedView = UIView()
   selectedView.backgroundColor = UIColor(red: 250/255, green: 250/255, blue:     250/255, alpha: 1.0)
   cell.selectedBackgroundView = selectedView

   return cell
}
Run Code Online (Sandbox Code Playgroud)

将颜色更改为任何您喜欢的颜色。


hha*_*amm 5

为了保持代码清洁,您应该考虑将您的单元格的屏幕设计相关代码移动UITableViewControllerUITableViewCell类中.

您的UITableViewController只需要设置单元格的选定状态,如下所示:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    guard let cell = tableView.cellForRow(at: indexPath) else { return }
    cell.setSelected(true, animated: true)
}
Run Code Online (Sandbox Code Playgroud)

您可以UITableViewCell通过覆盖在所创建的类中实现所需的自定义var isSelected.使用此解决方案,您可以为每个单元格选择不同的颜色.

class MyTableViewCell: UITableViewCell
{
    @IBOutlet weak var label:UILabel!

    override var isSelected: Bool
    {
        didSet{
            if (isSelected)
            {
                self.backgroundColor = UIColor.red
                if let label = label
                {
                    label.textColor = UIColor.white
                }
            }
            else
            {
                self.backgroundColor = UIColor.white
                if let label = label
                {
                    label.textColor = UIColor.black
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Tun*_*Fam 5

正确的方法(更自然和更自然):

override func awakeFromNib() {
    super.awakeFromNib()
    selectedBackgroundView = UIView()
    selectedBackgroundView?.backgroundColor = .blue
}
Run Code Online (Sandbox Code Playgroud)

为什么其他方法是错误的:

  1. 突出显示解决方案:未选中突出显示。高光不会像选择一样进行动画处理。感觉不像正常的选择那样自然
  2. 无需在setSelectedmethod中更改selectedBackgroundView的颜色,因为iOS会为我们处理动画。
  3. 设置的backgroundColor行为也与iOS不同