2012-01-24 23 views

उत्तर

1

आप तालिका सेल निर्माण विधि में कोड की निम्न पंक्ति कोशिश कर सकते हैं -

cell.textLabel.backgroundColor = //Your color; 
cell.detailTextLabel.backgroundColor = //Your color; 

आप और अधिक विस्तार वर्णन है, जहां आप कस्टम sectioned तालिका आप क्या उल्लेख किया है के समान बनाने का विस्तार उदाहरण मिल सकता है के लिए निम्नलिखित उल्लेख कर सकते हैं - http://undefinedvalue.com/2009/08/25/changing-background-color-and-section-header-text-color-grouped-style-uitableview

+0

मैं अनुभाग के शीर्षक को सेल लेबल नहीं बदलना चाहता हूं। –

+0

का अर्थ तालिका अनुभाग का शीर्षलेख है। – rishi

+0

हां, मेरा मतलब हेडर लेबल था। –

5

tableViewController में tableView:viewForHeaderInSection: विधि को लागू करें। इससे आपको हेडर के लिए अपना खुद का दृश्य प्रदान करने की अनुमति मिल जाएगी, जिसमें यूआईएलएबल शामिल हो सकता है जो आप चाहते हैं कि प्रारूपण, उदाहरण के लिए

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UILabel *customLabel = [[UILabel alloc] init]; 
    customLabel.text = [self tableView:tableView titleForHeaderInSection:section]; 
    return customLabel; 
} 

लेबल फ्रेम को अनुभागों के बाहर स्थान के लिए पर्याप्त लंबा सेट करना याद रखें। आप लेबल को एक बड़े UIView के अंदर एम्बेड करना चाहते हैं और इसके बजाय स्थिति को सरल बनाने के लिए वापस लौटना चाहते हैं (उदाहरण के लिए यदि आप लेबल पर बाएं-पैडिंग को बढ़ाएं)।

+0

यह एक रिसाव है। रिलीज या autorelease कस्टम लेबल। – stephen

+2

मुझे लगता था कि वह एआरसी का उपयोग कर रहा था। –

1

मैं इस पाया: How to change text color for Section Headers in a Grouped TableView in iPhone SDK? यह एक का इलाज काम करता है, और मैं @rishi द्वारा जवाब में लिंक से कोड का एक छोटा सा के साथ संयुक्त, और यह मेरे जीवन इतना आसान बना दिया !! हालांकि मुझे लेबल दृश्य के समन्वय को थोड़ा सा ट्विक करना पड़ा, लेकिन यह एक आकर्षण की तरह काम करता था।

54

यह एक पुराना सवाल है, लेकिन मुझे लगता है कि उत्तर को अद्यतन करने की आवश्यकता है।

इस विधि में आपके स्वयं के कस्टम व्यू को परिभाषित करने और बनाने में शामिल नहीं है। iOS 6 में और ऊपर, आप आसानी से

-(void)tableView:(UITableView *)tableView 
    willDisplayHeaderView:(UIView *)view 
    forSection:(NSInteger) 

प्रतिनिधि विधि को परिभाषित करते हुए पृष्ठभूमि रंग और पाठ का रंग बदल सकते हैं।

उदाहरण के लिए:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section 
{ 
    // Background color 
    view.tintColor = [UIColor blackColor]; 

    // Text Color 
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view; 
    [header.textLabel setTextColor:[UIColor whiteColor]]; 

    // Another way to set the background color 
    // Note: does not preserve gradient effect of original header 
    // header.contentView.backgroundColor = [UIColor blackColor]; 
} 

मेरी पोस्ट यहाँ से लिया: https://happyteamlabs.com/blog/ios-how-to-customize-table-view-header-and-footer-colors/

+2

अच्छी नौकरी, जिस तरह से यह विधि अभी भी आईओएस 7 में काम करती है, वैसे भी किया जाता है। –

+0

यह कोड आईओएस 7 के साथ "बस काम करता है" में छोड़ देता है। अच्छा। – russes

+0

अब तक का बेहतर उत्तर। –

2

यह मेरे स्विफ्ट को यह "अनुवाद" करने के लिए कुछ मिनट लग गए, लेकिन यहां स्विफ्ट 1.2 (आईओएस 8 में एक काम बराबर है)। सुनिश्चित करें और UITableViewDelegate को अपनी कक्षा में लागू करें:

// MARK: - VIEW METHODS 
override func viewDidLoad() { 
    tableView.delegate = self 
} 

// MARK: - TABLEVIEW DELEGATE METHODS 
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 
    let header = view as! UITableViewHeaderFooterView 
    header.textLabel.textColor = UIColor.whiteColor() 
} 
संबंधित मुद्दे