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

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

iOS crifan 2617浏览 0评论

想要设置UITableView的cell的选中后的颜色

此处,没有设置,通过viewDidLoad中的代码去选中:

        if self.curSelectIdx != InvalidSelectIndex {

           let selectedIndexPath = NSIndexPath(forRow: self.curSelectIdx, inSection: 0)

            gLog.verbose("selectedIndexPath=\(selectedIndexPath) self.curSelectIdx=\(self.curSelectIdx)")

            self.selectTableView.selectRowAtIndexPath(selectedIndexPath, animated: false, scrollPosition: UITableViewScrollPosition.Middle)

        }

默认颜色是灰色的:

想要换个颜色

swift uitableviewcell selection color

Custom UI TableViewCell selected backgroundcolor swift – Stack Overflow

ios – Swift – UITableViewCell Selected Background Color on Multiple Selection – Stack Overflow

ios – UITableView Cell selected Color? – Stack Overflow

How to give UITableViewCells a selected color other than grey – Swift 2 example code

最后,根据此处的特殊需求,改为:

    override func viewDidLoad() {

        super.viewDidLoad()

        if self.curSelectIdx != InvalidSelectIndex {

           let selectedIndexPath = NSIndexPath(forRow: self.curSelectIdx, inSection: 0)

            gLog.verbose("selectedIndexPath=\(selectedIndexPath) self.curSelectIdx=\(self.curSelectIdx)")

            self.selectTableView.selectRowAtIndexPath(selectedIndexPath, animated: false, scrollPosition: UITableViewScrollPosition.Middle)

        }

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

        let curCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: SelectItemTableViewCellId)

        curCell.selectionStyle = .None

        if indexPath.row == self.curSelectIdx {

            curCell.contentView.backgroundColor = CommonButtonColor

        }

        return curCell

    }

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

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

//

//        if let cell = tableView.cellForRowAtIndexPath(indexPath) {

//            cell.contentView.backgroundColor = CommonButtonColor

//        }

//    }

//    

//    func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath) {

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

//

//        if let cell  = tableView.cellForRowAtIndexPath(indexPath) {

//            cell.contentView.backgroundColor = .clearColor()

//        }

//    }

注意:

1.viewDidLoad中的selectRowAtIndexPath不会触发:

didSelectRowAtIndexPath

或:

didHighlightRowAtIndexPath

的。

2.此处单机选中某个cell后,页面就退出了,

所以没去考虑选中时候的cell的背景色。

效果如下:

转载请注明:在路上 » [已解决]swift在初始化时设置表格单元选中后的颜色

发表我的评论
取消评论

表情

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

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