UITableViewCell selectionStyle未设置为.default

时间:2017-12-18 12:16:35

标签: ios swift uitableview swift3 uikit

我在应用中将UITableViewCellSelectionStyle设置为blue时遇到问题。不知何故,每次都会将值覆盖到gray

我的故事板文件中没有TableViewController。实际上在代码中调用了以下行,但结果颜色为灰色:

cell.selectionStyle = .default

TableViewController.swift(完成):

import UIKit

typealias FilterSelectionHandler = (_ returnValue:String?) -> Void

@objc class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIPopoverPresentationControllerDelegate {

    var values:[String] = []
    var preSelectedValue: String = ""
    var selectedIndex = 99
    var selectionHandler: FilterSelectionHandler?
    let width:CGFloat = 300
    let height: CGFloat = 180

    fileprivate var tableView: UITableView!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        self.tableView.sizeToFit()
        self.tableView.frame = CGRect(x: self.tableView.frame.origin.x, y: self.tableView.frame.origin.y, width: self.width, height: self.tableView.bounds.height + 20)
        self.preferredContentSize = self.tableView.bounds.size
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.translatesAutoresizingMaskIntoConstraints = false

        //// tableview
        self.tableView = UITableView(frame:self.view.frame)
        self.tableView.translatesAutoresizingMaskIntoConstraints = false
        self.tableView!.dataSource = self
        self.tableView!.delegate = self
        self.tableView.separatorStyle = .singleLine
        self.tableView.isScrollEnabled = false;
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "DefaultCell")
        self.view.addSubview(self.tableView)

        //// constraints
        self.tableView.leftAnchor.constraint(equalTo: view.leftAnchor,
                                                          constant: 0).isActive = true
        self.tableView.rightAnchor.constraint(equalTo: view.rightAnchor,
                                                           constant: 0).isActive = true
        self.tableView.topAnchor.constraint(equalTo: view.topAnchor,
                                                          constant: 0).isActive = true
        self.tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor,
                                                           constant: 0).isActive = true
        // default selection
        for myValue:String in self.values {
            if self.preSelectedValue == myValue {
                self.selectedIndex = self.values.index(of: myValue)!
                break
            }
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "DefaultCell", for: indexPath) as UITableViewCell

        if (indexPath.row == self.selectedIndex) {
            cell.setSelected(true, animated: false)
        }

        cell.textLabel?.text = values[(indexPath as NSIndexPath).row]

        cell.selectionStyle = .default // Default (blue) selection style is not being set

        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.values.count
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectionHandler?(self.values[(indexPath as NSIndexPath).row])
        dismiss(animated: true, completion: nil)
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let label: TableHeaderLabel = TableHeaderLabel()
        label.text = "Title"
        label.backgroundColor = UIColor(red:(46 / 255), green:(83 / 255), blue:(155 / 255), alpha:1);
        label.textColor = UIColor.white;
        return label
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "void"
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 30.0
    }

    func selectWasDone (_ selectionHandler:@escaping FilterSelectionHandler){
        self.selectionHandler = selectionHandler
    }
}

0 个答案:

没有答案