【问题标题】:UITableView backgroundColor and UITableView.appearanceUITableView backgroundColor 和 UITableView.appearance
【发布时间】:2015-11-10 02:44:30
【问题描述】:

我正在尝试创建一个包含 UITableView 的简单视图。 我想通过表视图的 backgroundColor 属性将表视图的背景颜色更改为紫色。 Apple 文档说您只需将 backgroundView 属性设置为 nil 即可工作。就我而言,它不起作用。

我必须调用 UITableView.appearance().backgroundColor = UIColor.purpleColor() 让它工作而不仅仅是 UITableView.backgroundColor = UIColor.purleColor()

我的问题是为什么?

这是代码:

class TaskTableView: UIView {
    var tableView = UITableView(frame: CGRectZero, style: .Plain)

    override init(frame: CGRect) {
        super.init(frame: frame)

        tableView.backgroundView = nil
        tableView.backgroundColor = UIColor.purpleColor()
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        tableView.translatesAutoresizingMaskIntoConstraints = false
        addSubview(tableView)

        let views = ["table": tableView]
        addConstraints(
            NSLayoutConstraint.constraintsWithVisualFormat("|[table]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)
        )

        addConstraints(
            NSLayoutConstraint.constraintsWithVisualFormat("V:|[table]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)
        )
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError()
    }


}

【问题讨论】:

    标签: ios uitableview cocoa-touch


    【解决方案1】:

    以编程方式创建新视图(自定义子类TaskTableView)时,上面提供的示例代码对我来说很好:

    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
    
            let tableViewFrame : CGRect = CGRectMake(20, 20, 100, 300)
            let taskTableView  : TaskTableView = TaskTableView.init(frame: tableViewFrame)
    
            view.addSubview(taskTableView)
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    }
    

    当在一个空白的新项目中运行此代码时,使用问题中提供的 TaskTableView 的类定义,结果是一个具有所需紫色背景颜色的表格视图。

    话虽如此,我可以猜测为什么您在设置表格视图的背景颜色时遇到问题。问题可能与视图生命周期有关。外观协议在视图出现在屏幕上之前应用于视图。您可能在视图生命周期中过早地实例化自定义视图实例,即在包含视图的视图控制器从其 nib 唤醒之前。因此,您尝试在尚未反序列化的视图上设置属性,因此不会显示所需的背景颜色设置。请参阅 Apple 开发者文档中的以下引用。

    如果您计划从 nib 文件加载自定义视图类的实例,您应该知道在 iOS 中,nib 加载代码不使用 initWithFrame: 方法来实例化新的视图对象。相反,它使用 initWithCoder: 方法,该方法是 NSCoding 协议的一部分。

    (参考:View Programming Guide for iOS

    值得指出的是,UITableView 类通常仅作为子视图添加到 UIViewController,而同一个视图控制器还充当 Table View 的委托和数据源。 Apple 建议您使用他们预先配置的 UITableViewController,它是一个 UIViewController,以 UITableView 作为其视图,同时也充当数据源和委托。将 UITableView 直接添加为自定义 view 类的子视图(如您所做的那样)不是最佳实践,因此不鼓励。

    有关使用 UITableView 类的更多信息,请参阅 Apple 官方文档:

    【讨论】:

    • "外观协议在视图出现在屏幕上之前应用到它们。"基本上这意味着它会覆盖我的初始设置
    猜你喜欢
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    相关资源
    最近更新 更多