2015-06-06 17 views

Trả lời

42

Đầu tiên thiết lập màu nền của tableView trong viewDidLoad như dưới đây:

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

Bây giờ thêm phương pháp này:

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

trong Swift 3, sử dụng dưới đây phương pháp thay vì trên một:

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

Tuyệt vời! Cảm ơn! Nó làm việc, không chắc chắn lý do tại sao nó không hoạt động với Storyboard .. Tôi chỉ cố gắng ở đó ... nhờ sự giúp đỡ .. –

+0

@Homam Tôi có một tableview tĩnh nhưng nó không làm việc cho tôi. – Nicholas

+0

@Nicholas Trong phương thức viewDidLoad() thêm câu lệnh sau: self.tableView.delegate = self – Homam

8
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 
} 
4

Nếu bạn muốn đạt được điều đó thông qua kịch bản sau đó chọn "Xem nội dung" của "xem bảng di động" trong tableView của bạn sau đó trong thuộc tính Inspector đi để xem và thay đổi màu sắc của nó như sau:

Xcode

1

sử dụng mã này cho sự thay đổi tableView màu

override func viewDidLoad() { 
     super.viewDidLoad() 

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

cho sự thay đổi tableView màu tế bào

cell.backgroundColor = UIColor.clearColor() 
Các vấn đề liên quan