【问题标题】:Setting TableViewCell Label colour on row selection Swift 3在行选择Swift 3上设置TableViewCell标签颜色
【发布时间】:2017-01-04 03:42:06
【问题描述】:

我有一个 TableView,该表是从一个数组中填充的。该单元格的类型为 TableViewCell.xib。我想更改单元格中标签的颜色。

这是我的 TableViewController


struct cell_data {
    let label1: String!
}

class TableViewController: UITableViewController {

    let cellDataArray = [cell_data]([cell_data(label1: "This text is for label 1")])

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cellDataArray.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = Bundle.main.loadNibNamed("TableViewCell", owner: self, options: nil)?.first as! TableViewCell

        cell.label_1.text = cellDataArray[indexPath.row].label1
        cell.selectionStyle = .none

        let whiteRoundedView : UIView = UIView(frame: CGRect(x:10, y:5, width: self.view.frame.size.width - 20, height: cell.frame.size.height - 7))
        whiteRoundedView.layer.backgroundColor = UIColor.green.cgColor
        whiteRoundedView.layer.masksToBounds = false
        whiteRoundedView.layer.shadowOffset = CGSize(width: -1, height: 1)
        whiteRoundedView.layer.shadowOpacity = 0.3

        cell.contentView.addSubview(whiteRoundedView)
        cell.contentView.sendSubview(toBack: whiteRoundedView)

        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let cell = tableView.cellForRow(at: indexPath)


    }

    override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath)


    }
}

我想要做的就是在我选择一行时改变单元格标签的颜色而不影响单元格内​​的 UIView。也就是说,一旦我改变了高度当我选择一行时,我尝试重新加载 UIView,但重新加载单元格会将 UIView 的高度设置为原始设置。

一旦我设置了标签的颜色,我想在取消选择行时将其设置回原来的颜色。

我希望我说得足够清楚。提前致谢。

【问题讨论】:

  • 你不能在 tableView:didSelectRowAt: 中将标签更改为你想要的颜色并在 tableView:didDeselectRowAt: 中重置它吗?
  • @ubiAle 否。当我更改 didSelectRowAt 中的标签时,我需要重新加载该行,这意味着它还会重新加载 UIView,并且在我选择时我会丢失对其所做的所有更改。跨度>
  • 为什么需要重新加载行?
  • @ubiAle 我还能如何更新标签本身?

标签: ios swift uitableview uiview


【解决方案1】:

您无需重新加载单元格即可更新标签的颜色。您可以尝试使用以下代码覆盖 TableViewCell 中的方法:

override func setSelected(selected: Bool, animated: Bool) {
 super.setSelected(selected, animated: animated)
 if (selected) {
   //Change your label color for selected state
 } else {
   //Change your label color for unselected state
 }
}

【讨论】:

  • OP 还想增加单元格内视图的高度。为此,他必须重新加载单元格!
  • 类似于可扩展单元格?如果是这种情况,他可以重新加载单元格以增加内部视图的高度和单元格高度本身。通过保存 selectedIndexPath 并尝试在重新加载单元格后以编程方式选择该单元格。
  • @Rikh 忘了提到高度会随着我当前的代码更新而无需重新加载,因为tableView.updateBegin
【解决方案2】:

试试这个:

 func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) 

   {
      let cell = tableView.cellForRowAtIndexPath(indexPath)! as! customTableViewCell // name of your custom cell class
      cell.label_1.backgroundColor = UIColor.whiteColor()//give your selection color

    }

【讨论】:

    【解决方案3】:

    试试这个

    override func tableView(_ tableView: UITableView, didDeselectRowAt    indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath) as yourCell
        cell.yourlable.textcolor = yourColor;
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-02-04
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多