迅速。选中后更改自定义Tableview单元格标签的颜色

时间:2015-06-24 10:08:52

标签: ios swift uitableview custom-cell didselectrowatindexpath

我在swift中有一个自定义Tableview单元格,在该单元格中有一个标签。

我希望能够在您选择单元格时更改标签。

如何在UITableviewCell

中引用自定义didSelectRowAtIndexPath标签

在Objective C中引用didSelectRowAtIndexPath中的自定义单元格,我会使用以下内容:

MPSurveyTableViewCell *cell = (MPSurveyTableViewCell *)[tableViewcellForRowAtIndexPath:indexPath];
cell.customLabel.TextColor = [UIColor redColor]; 

我必须在swift中做些什么才能达到相同的效果?

6 个答案:

答案 0 :(得分:12)

您只需将相同的代码翻译为Swift即可。

var myCell = tableView.cellForRowAtIndexPath(indexPath) as! MPSurveyTableViewCell
myCell.customLabel.textColor = UIColor.redColor()

答案 1 :(得分:6)

以上答案不完整

因为UITableView会重复使用单元格,所以需要检查单元格是否被选中并在cellForRowAtIndexPath中适当调整颜色。可能存在拼写错误,但这是完整的答案:

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

    let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifierHere", forIndexPath: indexPath) as! MPSurveyTableViewCell 

    // setup your cell normally

    // then adjust the color for cells since they will be reused
    if cell.selected {
        cell.customLabel.textColor = UIColor.redColor()
    } else {
        // change color back to whatever it was
        cell.customLabel.textColor = UIColor.blackColor()
    }

    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
    let cell = tableView.cellForRowAtIndexPath(indexPath) as! MPSurveyTableViewCell
    cell.customLabel.textColor = UIColor.redColor()
    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)

}

func tableView(tableView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath) as! MPSurveyTableViewCell

    // change color back to whatever it was
    cell.customLabel.textColor = UIColor.blackColor()
    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)

}

答案 2 :(得分:4)

swift 3 xcode 8

func  tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    let Cell = tableView.cellForRow(at: indexPath)
    Cell?.textLabel?.textColor = UIColor.red // for text color
    Cell?.backgroundColor = UIColor.red // for cell background color
}

答案 3 :(得分:2)

试试这个,

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    var Cell = tableView.cellForRowAtIndexPath(indexPath) as! MPSurveyTableViewCell
  Cell. customLabel.textColor = UIColor. redColor()
}

答案 4 :(得分:2)

为什么不在UITableViewCell子类中使用setSelected?

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
  customLabel.textColor = selected ? UIColor.red : UIColor.black
}

或者如果您希望它在特定时间后返回取消选择的颜色:

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
  if selected {
  customLabel.textColor = UIColor.red
  DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2, execute: { [weak self] in
    self?.customLabel.textColor = UIColor.black
    }
  }
}

然后只需设置您的单元格selectionStyle = .none

答案 5 :(得分:0)

let CellObject = tableView.dequeueReusableCell(withIdentifier: "your cell identifier name") as! MPSurveyTableViewCell
CellObject.customLabel.textColor = UIColor.red
相关问题