2015-06-06 16 views

Antwort

42

zuerst die Hintergrundfarbe des Tableview in viewDidLoad wie im folgenden beschrieben:

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

Jetzt diese Methode hinzufügen:

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

in Swift 3 unten verwenden Methoden anstelle von oben ein:

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

Great! Vielen Dank! Es funktionierte, nicht sicher, warum es nicht mit Storyboard funktioniert .. Ich habe es nur versucht ... danke für die Hilfe .. –

+0

@Homam Ich habe eine statische Tabellenansicht, aber es funktioniert nicht für mich. – Nicholas

+0

@Nicholas In viewDidLoad() -Methode fügen Sie diese Aussage hinzu: 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

Falls Sie über Storyboard erreichen wollen, dann „Inhalt anzeigen“ wählen Sie aus „Tabellenansicht Zelle“ in der Tableview dann in geht Attribut Inspektor zum ansehen und seine Farbe zu ändern, wie so:

Xcode

1

Verwendung dieses Code für den Wandel Tableview Farbe

override func viewDidLoad() { 
     super.viewDidLoad() 

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

für Veränderung Tableview Zelle Farbe

cell.backgroundColor = UIColor.clearColor() 
Verwandte Themen