![](/img/trans.png)
[英]Change label color when collectionView Cell is selected in Swift iOS
[英]Swift. Change Color of Custom Tableview Cell Label when selected
我在swift中有一个Custom Tableview
单元格,在该单元格中有一个标签。
我希望能够在选择单元格时更改标签。
如何在didSelectRowAtIndexPath
引用我的自定义UITableviewCell
标签
在Objective C中,在didSelectRowAtIndexPath
引用我的自定义单元格,我将使用以下内容:
MPSurveyTableViewCell *cell = (MPSurveyTableViewCell *)[tableViewcellForRowAtIndexPath:indexPath];
cell.customLabel.TextColor = [UIColor redColor];
我必须在swift中做些什么才能达到相同的效果?
您只需将相同的代码转换为Swift即可。
var myCell = tableView.cellForRowAtIndexPath(indexPath) as! MPSurveyTableViewCell
myCell.customLabel.textColor = UIColor.redColor()
以上答案不完整
因为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)
}
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
}
试试这个,
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
var Cell = tableView.cellForRowAtIndexPath(indexPath) as! MPSurveyTableViewCell
Cell. customLabel.textColor = UIColor. redColor()
}
为什么不在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
let CellObject = tableView.dequeueReusableCell(withIdentifier: "your cell identifier name") as! MPSurveyTableViewCell
CellObject.customLabel.textColor = UIColor.red
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.