UITableView 背景颜色 Swift

Posted

技术标签:

【中文标题】UITableView 背景颜色 Swift【英文标题】:UITableView Background Color Swift 【发布时间】:2016-01-11 01:37:08 【问题描述】:

我希望使用 Swift 更改 UITableView 的背景颜色。我想让单元格背景和实际表格的背景都成为特定颜色。到目前为止,我已经用这段代码创建了一个简单的表:

import UIKit

class MyTripsViewController: UIViewController, UITableViewDelegate 

    override func viewDidLoad() 
        super.viewDidLoad()
    

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
        return 2
    

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

        let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
        cell.textLabel?.text = "Test"
        return cell
    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
    

【问题讨论】:

这对***.com/questions/12617913/…有帮助吗? 【参考方案1】:

单元格属性backgroundColor 更改背景颜色。设置颜色:

cell.backgroundColor = UIColor(red: 0.1, green: 0.2, blue: 0.3, alpha: 1.5)

或者使用预定义的颜色:

cell.backgroundColor = UIColor.blueColor()

【讨论】:

这将有助于改变单元格的颜色,但是这也会改变单元格后面表格的颜色吗?我想更改颜色,以便即使表格中没有单元格的区域也是其他颜色。 tableView 也是一样。 self.tableView.backgroundColor = UIColor.greenColor()

以上是关于UITableView 背景颜色 Swift的主要内容,如果未能解决你的问题,请参考以下文章