【问题标题】:UITableView Background Color SwiftUITableView 背景颜色 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()
    }
}

【问题讨论】:

标签: ios swift uitableview


【解决方案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()
猜你喜欢
  • 2011-10-07
  • 2014-09-04
  • 2015-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-12
  • 2011-03-25
  • 1970-01-01
相关资源
最近更新 更多