最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift给UITableViewCell选中之前的按下的时候加上背景色

iOS crifan 2357浏览 0评论

之前是已经实现了:

给选中的cell添加背景色:

[已解决]swift在初始化时设置表格单元选中后的颜色

但是,对于选中之前,按下去的那一刻,是没有背景色的:

swift uitableviewcell before select add background color

ios – How to change color of UITableViewCell when selecting? – Stack Overflow

    // Selection

    // -tableView:shouldHighlightRowAtIndexPath: is called when a touch comes down on a row.

    // Returning NO to that message halts the selection process and does not cause the currently selected row to lose its selected look while the touch is down.

    @available(iOS 6.0, *)

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

    @available(iOS 6.0, *)

    optional public func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath)

    @available(iOS 6.0, *)

    optional public func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath)

去添加didHighlightRowAtIndexPath,给cell设置背景色:

let CommonButtonColor:UIColor = UIColor(hexString: "#CC0486")!

    func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) {

        gLog.verbose("tableView=\(tableView), indexPath=\(indexPath)")

        if let curCell = tableView.cellForRowAtIndexPath(indexPath) {

            setSelectedColor(curCell)

        }

    }

    func setSelectedColor(curCell:UITableViewCell) {

        curCell.contentView.backgroundColor = CommonButtonColor

        curCell.textLabel?.textColor = UIColor.whiteColor()

    }

即可实现对应的效果:

转载请注明:在路上 » [已解决]swift给UITableViewCell选中之前的按下的时候加上背景色

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.165 seconds, using 22.14MB memory