UITableView 复选标记样式删除选择上的突出显示

hai*_*ham 3 uitableview uicolor ios swift uitableviewrowaction

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

当我在 cellForRowAtIndexPath 中尝试此操作时:

    cell.backgroundView = UIView()
    cell.backgroundView?.backgroundColor = UIColor.clearColor()
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

当我在 cellForRowAtIndexPath 中尝试此操作时:

    cell.selectionStyle = UITableViewCellSelectionStyle.None
Run Code Online (Sandbox Code Playgroud)

它不再显示复选标记。

更新:尝试过这个,与 cell.selectionStyle 做同样的事情,它不再做复选标记

func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return false
}
Run Code Online (Sandbox Code Playgroud)

这样做的好方法是什么?我希望它仍然像复选框一样工作,但只是不希望出现蓝色突出显示。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)
Run Code Online (Sandbox Code Playgroud)

表格视图功能:

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)!
}
Run Code Online (Sandbox Code Playgroud)

小智 5

为了保留复选标记,您不能将此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
}
Run Code Online (Sandbox Code Playgroud)