Подтвердить что ты не робот

IOS Swift - Как изменить цвет фона в виде таблицы?

У меня есть простой табличный вид, я могу изменить цвета ячеек, но при попытке изменить цвет Table View (Background part) он не работает... Я попробовал его через Storyboard... Может кто-то угодить помощь

4b9b3361

Ответ 1

Сначала установите цвет фона таблицыView в viewDidLoad, как показано ниже:

override func viewDidLoad() {
    super.viewDidLoad()   
    self.tableView.backgroundColor = UIColor.lightGrayColor()
}

Теперь добавьте этот метод:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.backgroundColor = UIColor.clearColor()
}

В Swift 3 используйте ниже методы вместо одного:

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.backgroundColor = UIColor.lightGray
}

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.backgroundColor = UIColor.clear
}

Ответ 2

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.contentView.backgroundColor = UIColor.yellowColor()
}

Swift 3

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.contentView.backgroundColor = UIColor.yellow
}

Ответ 3

Если вы хотите достичь этого с помощью раскадровки, затем выберите "Просмотр содержимого" в ячейке таблицы, а затем в атрибуте "Инспектор" перейдите в "Просмотр" и измените его цвет следующим образом:

Xcode

Ответ 4

используйте этот код для изменения tableView color

override func viewDidLoad() {
        super.viewDidLoad()

       // define tableview background color
        self.tableView.backgroundColor = UIColor.clearColor()
}

для таблицы changeViewView color

cell.backgroundColor = UIColor.clearColor()