【问题标题】:UITableView checkmark style remove highlight on selectUITableView 复选标记样式在选择时删除突出显示
【发布时间】:2015-07-13 17:59:32
【问题描述】:

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

当我在 cellForRowAtIndexPath 中尝试这个时:

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

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

当我在 cellForRowAtIndexPath 中尝试这个时:

    cell.selectionStyle = UITableViewCellSelectionStyle.None

它不再显示复选标记。

更新:试过这个,和 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)!
}

【问题讨论】:

    标签: ios swift uitableview uicolor uitableviewrowaction


    【解决方案1】:

    为了保留复选标记,您不能将这个 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
    }
    

    【讨论】:

      【解决方案2】:

      Swift 1.2 使用:

      tableView.deselectRowAtIndexPath(indexPath, animated: true)

      在 didSelectRowAtIndexPath 委托方法中。

      【讨论】:

        【解决方案3】:

        以上建议都不适合我。在 Swift 5 中,将此行添加到您的 cellForRowAtIndexPath 函数中:

        cell.selectionStyle = UITableViewCell.SelectionStyle.none
        

        【讨论】:

          【解决方案4】:

          有一个官方的方法可以用 UITableView 来做到这一点,那就是使用这个:

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

          如果您为此方法返回 YES,那么当单击该单元格时,tableview 将突出显示一个单元格。

          还请注意,如果您不想使用它,则需要更改 contentView.backgroundColor,而不仅仅是单元格的背景颜色。但是高亮路线是最好走的路线。

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

          【讨论】:

          • 刚刚试过这个。功能与更改不再选择单元格的 cell.selectionStyle 属性相同(它不显示复选标记并且不调用 didSelectRow 方法
          • 也改变 cell.contentView 属性没有做任何事情
          • 不要在这里返回 false - 如我所说返回 YES。还可以尝试更改单元格的 selectionStyle 属性 - 这里有很多选项用于选择单元格时的外观 - 例如 UITableViewCellSelectionStyleGray 将使单元格在选择时变为灰色。
          • 试过这个:cell.selectionStyle = UITableViewCellSelectionStyle.Gray 但它仍然选择为蓝色。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-19
          • 2014-07-23
          • 1970-01-01
          • 1970-01-01
          • 2021-05-03
          • 1970-01-01
          相关资源
          最近更新 更多