首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Swift中以编程方式突出显示UITableViewCell

在Swift中以编程方式突出显示UITableViewCell
EN

Stack Overflow用户
提问于 2018-03-27 04:19:23
回答 3查看 5.9K关注 0票数 2

如何以编程方式突出显示表格视图单元格?

我正在使用:

代码语言:javascript
运行
复制
tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
tableView.delegate?.tableView!(self.ordersTableView, didSelectRowAt: indexPath)

我遇到的问题是单元格被选中(正在执行didSelectRowAt中指定的所有操作),但单元格现在高亮显示。它不会显示为选中状态。

我想实现的功能类似于iPad上的设置应用程序。启动应用程序后,您会看到"general“单元格已被选中并突出显示。

当用户触摸时,单元格将正确突出显示。

我甚至尝试在我的UITableViewCell子类中覆盖isSelected变量。

好吧,事实证明这是后台任务的问题。在我选择单元格之后,单元格被再次加载,这就是为什么选择不可见的原因。

EN

回答 3

Stack Overflow用户

发布于 2018-03-27 17:31:42

注意::你的问题标题说,你有一个单元格突出显示的查询,而你的问题描述说,你有一个单元格选择问题。两者是不同的事件。

这是一个答案,如何在表格视图行(单元格)选择上管理(设置颜色):

代码语言:javascript
运行
复制
// 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
    }
}
票数 3
EN

Stack Overflow用户

发布于 2018-03-27 13:58:27

试试这个:-

代码语言:javascript
运行
复制
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
var selectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
selectedCell.contentView.backgroundColor = UIColor.redColor()
}
票数 0
EN

Stack Overflow用户

发布于 2018-03-27 18:02:23

下面是处理单元格背景选择的另一个选项

代码语言:javascript
运行
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49499948

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档