2015-06-06 7 views
22

मेरे पास एक साधारण टेबल दृश्य है, मैं कोशिकाओं के रंग बदल सकता हूं, लेकिन तालिका दृश्य (पृष्ठभूमि भाग) का रंग बदलने की कोशिश करते समय यह काम नहीं कर रहा है ... मैंने स्टोरीबोर्ड के माध्यम से इसे आजमाया ... क्या कोई मदद कर सकता हैआईओएस स्विफ्ट - टेबल व्यू के पृष्ठभूमि रंग को कैसे बदला जाए?

उत्तर

42

पहले नीचे की तरह viewDidLoad में tableView की पृष्ठभूमि का रंग सेट करें:

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() 
} 

स्विफ्ट 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

ग्रेट के लिए के लिए! धन्यवाद! यह काम करता है, यकीन नहीं है कि यह स्टोरीबोर्ड के साथ क्यों काम नहीं करता .. मैं केवल वहां कोशिश कर रहा था ... मदद के लिए धन्यवाद .. –

+0

@ होमाम मेरे पास एक स्थिर टेबलव्यू है लेकिन यह मेरे लिए काम नहीं करता है। – Nicholas

+0

@ निकोलस ViewDidLoad() विधि में यह कथन जोड़ें: self.tableView.delegate = self – Homam

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

स्विफ्ट 3

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

आप प्राप्त करने के लिए है कि स्टोरीबोर्ड के माध्यम से फिर चुनें की "तालिका दृश्य सेल" "सामग्री देखें" अपने tableView में तो में चाहते हैं विशेषता इंस्पेक्टर इसलिए की तरह देखें और इसके रंग बदलने के लिए जाना:

Xcode

1

उपयोग इस कोड परिवर्तन tableView रंग

override func viewDidLoad() { 
     super.viewDidLoad() 

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

परिवर्तन tableView सेल रंग

cell.backgroundColor = UIColor.clearColor() 
संबंधित मुद्दे

 संबंधित मुद्दे