Ios 在Swift中以编程方式突出显示UITableViewCell
如何以编程方式突出显示表视图单元格 我正在使用:Ios 在Swift中以编程方式突出显示UITableViewCell,ios,swift,tableview,cell,Ios,Swift,Tableview,Cell,如何以编程方式突出显示表视图单元格 我正在使用: tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none) tableView.delegate?.tableView!(self.ordersTableView, didSelectRowAt: indexPath) 我遇到的问题是,正在选择单元格(正在执行我的didSelectRowAt中指定的所有操作),但单元格现在确实高亮显示。它似乎没有被选中 我试
tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
tableView.delegate?.tableView!(self.ordersTableView, didSelectRowAt: indexPath)
我遇到的问题是,正在选择单元格(正在执行我的didSelectRowAt
中指定的所有操作),但单元格现在确实高亮显示。它似乎没有被选中我试图实现的是iPad上的设置应用程序。您启动应用程序后,会看到已选中并突出显示的“常规”单元格。
当用户触摸时,单元格会正确高亮显示 我甚至尝试过覆盖UITableViewCell子类中的
isSelected
变量
好的,原来这是后台任务的问题。在我选择单元格后,这些单元格再次加载,这就是为什么选择不可见的原因。请尝试以下操作:-
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
var selectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
selectedCell.contentView.backgroundColor = UIColor.redColor()
}
注意:您的问题标题显示,您有一个带有单元格突出显示的查询,您的问题描述显示,您的单元格选择有问题。两者都是不同的事件 下面是一个答案,如何管理(设置颜色)tableview行(单元格)选择:
// make sure your tableview row selection is enabled
yourTableViewInstance.allowsSelection = true
// handle highlighted background in data source method also
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "textview") as! YourTableViewCell
cell.contentView.backgroundColor = cell.isSelected ? UIColor.red : UIColor.gray
return cell
}
// didSelectRowAtIndexPath - change background color
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) as? YourTableViewCell {
cell.contentView.backgroundColor = UIColor.red
}
}
// didDeselectRowAtIndexPath - change background color
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) as? YourTableViewCell {
cell.contentView.backgroundColor = UIColor.gray
}
}
这里是处理单元格背景选择的另一个选项
class YourTableViewCell: UITableViewCell {
// override cell selection
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
if self.selectedBackgroundView == nil {
let bgView = UIView()
self.selectedBackgroundView = bgView
}
self.selectedBackgroundView?.backgroundColor = selected ? UIColor.red : UIColor.gray
}
}
// enable row/cell selection
yourTableViewInstance.allowsSelection = true
// handle highlighted background in data source method also
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "textview") as! YourTableViewCell
if self.selectedBackgroundView == nil {
let bgView = UIView()
self.selectedBackgroundView = bgView
}
self.selectedBackgroundView?.backgroundColor = selected ? UIColor.red : UIColor.gray
return cell
}
是否要说明单元格在以编程方式选择后不会高亮显示?请确保单元格的
selectionStyle
为UITableViewCellSelectionStyleBlue
,并且tableView的AllowSelection
设置为YES
。没有什么变化。