UITableView选中标记样式删除选中的高亮显示

时间:2015-07-13 17:59:32

标签: ios swift uitableview uicolor uitableviewrowaction

我在选择表格视图单元格时尝试删除突出显示,但希望保留显示的复选标记。

当我在cellForRowAtIndexPath中尝试这个时:

    cell.backgroundView = UIView()
    cell.backgroundView?.backgroundColor = UIColor.clearColor()

它仅删除复选标记下方的突出显示,而不是整行(请参阅附图)。

enter image description here

当我在cellForRowAtIndexPath中尝试这个时:

    cell.selectionStyle = UITableViewCellSelectionStyle.None

它不再显示复选标记。

UPDATE:试过这个,和cell.selectionStyle做同样的事情,它不再做复选标记

func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return false
}

这样做的好方法是什么?我希望它仍然像复选框一样运行,但只是不想发生蓝色突出显示。 TableView是动态生成的:

    checkBoxView = UITableView()
    checkBoxView.frame = CGRect(x: qView.bounds.midX, y: qView.bounds.midY, width: qView.bounds.width - 100, height: qView.bounds.height/1.5)
    checkBoxView.center = CGPointMake(qView.bounds.midX, qView.bounds.midY)
    checkBoxView.delegate = self
    checkBoxView.dataSource = self
    checkBoxView.tag = 100
    checkBoxView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    checkBoxView.setEditing(true, animated: true)
    self.qView.addSubview(checkBoxView)

表格视图功能:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.checkBoxContent.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell = checkBoxView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
    cell.textLabel?.text = self.checkBoxContent[indexPath.row]
    cell.backgroundColor = UIColor.clearColor()
    cell.tintColor = UIColor.greenColor()
    return cell
}
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
    return UITableViewCellEditingStyle(rawValue: 3)!
}

4 个答案:

答案 0 :(得分:3)

为了保留复选标记,您无法对此 shouldHighlightRowAtIndexPath 方法设置false。如果你这样做,这根本不会在左侧显示复选标记。

我所做的是改变" selectedBackgroundView"细胞将保持左侧复选标记并让我有机会设置背景颜色。我在这里附上一些代码并希望它会有所帮助。

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

let cell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath) as! RecordTableViewCell
cell.selectedBackgroundView = UIView(frame: cell.frame)
cell.selectedBackgroundView?.backgroundColor = UIColor.orangeColor()

return cell
}

答案 1 :(得分:0)

Theres是使用UITableView执行此操作的官方方法,即使用此方法:

   optional func tableView(_ tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool

如果为此方法返回YES,则tableview将在单击时突出显示单元格。

另请注意,如果您不想使用它,则需要更改contentView.backgroundColor,而不仅仅是单元backgroundColor。但突出的路线是最好的路线。

此处的文档:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:shouldHighlightRowAtIndexPath

答案 2 :(得分:0)

Swift 1.2使用:

tableView.deselectRowAtIndexPath(indexPath,animated:true)

在didSelectRowAtIndexPath委托方法中。

答案 3 :(得分:0)

以上所有建议对我而言都无济于事。在Swift 5中,将此行添加到cellForRowAtIndexPath函数中:

cell.selectionStyle = UITableViewCell.SelectionStyle.none