2010-05-24 6 views
7

मैंडिफ़ॉल्ट UIView को डिफ़ॉल्ट तालिका के समान बनाने के लिए कैसे करें दृश्य: viewForHeaderInSection :?

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 

लागू करने के लिए सफेद रंग के बजाय काले रंग की धारा में हेडर के पाठ लेबल पाने की कोशिश की है, लेकिन यह डिफ़ॉल्ट एक एसडीके द्वारा बनाई से इतना अलग लग रहा है, मेरा तो है बदसूरत।

एसआईवीके के समान ही UIView को फिर से कैसे बनाया जाए?

एप्पल प्रलेखन से:

Discussion
तालिका दृश्य के लिए अनुभाग शीर्ष लेख खिताब एक निश्चित फ़ॉन्ट शैली का उपयोग करता है। यदि आप एक अलग फ़ॉन्ट शैली चाहते हैं, तो प्रतिनिधि विधि तालिका में एक कस्टम दृश्य (उदाहरण के लिए, एक UILabel ऑब्जेक्ट) वापस देखें दृश्य: viewForHeaderInSection: इसके बजाय।

उत्तर

13

हालांकि यह बिल्कुल सही नहीं है, मैं आशा करता हूं कि कोई मेरे दृष्टिकोण पर सुधार कर सके। पाठ सही है (डिफ़ॉल्ट की तरह सफेद, लेकिन आप इसे बदल सकते हैं)। पृष्ठभूमि ढाल और अस्पष्टता सही नहीं है (बहुत हल्की ??) - यहां कुछ tweaking की आवश्यकता हो सकती है।

sectionheaderbackground.png यहाँ प्राप्त करें:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    return nil; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return 22; //returning 0 hides section headers 
} 

यह विशेष रूप से अगर उपयोगी हो सकता है: http://img5.imageshack.us/img5/6616/sectionheaderbackground.png

// Create a stretchable image that emulates the default gradient 
UIImage *buttonImageNormal = [UIImage imageNamed:@"sectionheaderbackground.png"]; 
UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0]; 

// Create the view for the header 
CGRect sectionFrame = CGRectMake(0.0, 0.0, 320.0, 22.0); 
UIView *sectionView = [[UIView alloc] initWithFrame:sectionFrame]; 
sectionView.alpha = 0.9; 
sectionView.backgroundColor = [UIColor colorWithPatternImage:stretchableButtonImageNormal]; 

// Create the label 
CGRect labelFrame = CGRectMake(10.0, 0.0, 310.0, 22.0); 
UILabel *sectionLabel = [[UILabel alloc] initWithFrame:labelFrame]; 
sectionLabel.text = @"Test section label"; 
sectionLabel.font = [UIFont boldSystemFontOfSize:18.0]; 
sectionLabel.textColor = [UIColor whiteColor]; 
sectionLabel.shadowColor = [UIColor grayColor]; 
sectionLabel.shadowOffset = CGSizeMake(0, 1); 
sectionLabel.backgroundColor = [UIColor clearColor]; 
[sectionView addSubview:sectionLabel]; 
[sectionLabel release]; 

// Return the header section view 
return sectionView; 
+0

आपके उत्तर के लिए धन्यवाद! मैं कोशिश करूंगा और आपको बता दूंगा। यहां की चाल मौजूदा छवि का उपयोग पृष्ठभूमि रंग के रूप में करना है :) मैंने सोचा कि वे नाम के साथ रंग का उपयोग करते हैं: पी –

+0

जैसा कि डेविड डी ने उत्तर दिया था, 'वापसी शून्य' सबसे मूल दिखती है। –

+0

निश्चित रूप से, वापसी शून्य सबसे मूल दिखती है, लेकिन यह आपको पाठ रंग जैसे कुछ भी अनुकूलित करने की अनुमति नहीं देती है, जो सवाल पूछ रहा था। – dferg

0

आप shadowColor और shadowOffset का उपयोग कर इसे नकल करने की कोशिश कर सकते हैं।

मैं निम्नलिखित की तरह कोड का उपयोग कर उदाहरण के लिए काले रंग में यह किया है:

label.shadowColor = [UIColor whiteColor]; 
label.shadowOffset = CGSizeMake(1, 1); 

रंग और छाया संयोजनों के साथ खिलवाड़ करके, आप बहुत अच्छी तरह से लेबल की नकल कर सकते हैं।

+0

हाय रिक, कैसे पृष्ठभूमि के बारे में? मुझे किस रंग का उपयोग करना चाहिए? –

+0

क्या आप इसे समूहीकृत तालिका दृश्य में डाल रहे हैं? यदि ऐसा है तो आपके पास [UIColor clearColor] का पृष्ठभूमि रंग होना चाहिए; – rickharrison

+0

नहीं, रिक, यह वही नहीं है जो मैं चाहता हूं। जैसा कि मैंने प्रश्न में उद्धृत किया है, ऐप्पल प्रलेखन की चर्चा से, मैं कस्टम दृश्य को दोबारा बनाना चाहता हूं जो डिफ़ॉल्ट सफेद रंग की बजाय काले रंग में पाठ रंग के साथ ऐप्पल से डिफ़ॉल्ट अनुभाग शीर्षलेख शीर्षक के समान है। समस्या यह है कि डिफ़ॉल्ट पृष्ठभूमि बहुत खास है, मुझे नहीं पता कि कौन सा रंग है, और ऐसा लगता है कि यह ढाल है। –

1

आप अपने tableview कक्षा में निम्न विधियों को लागू करने से डिफ़ॉल्ट UITableView अनुभाग शीर्ष लेख प्राप्त कर सकते हैं आप अपने ऐप में एक UISearchDisplayController का उपयोग करते हैं, जहां आप खोज परिणामों को एक साधारण UITableViewStylePlain फैशन में प्रदर्शित करना चाहते हैं, मान लें कि, आपके custo के मुख्य मुख्यदृश्य का आकार। इसके बाद आप कुछ इस तरह लागू हो सकते हैं:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    if ([self.searchDisplayController.searchBar isFirstResponder]) return nil; 
    //create and return your custom UIView here 
} 
+0

यह ओपी को पाठ के रंग को बदलने में मदद नहीं करता – user102008

+0

यह सबसे प्रासंगिक उत्तर है! यदि आप डिफ़ॉल्ट दिखने वाले अनुभाग शीर्षलेख के साथ वापस लौटना चाहते हैं, तो बस शून्य वापस करें। पारितोषिक के लिए धन्यवाद! –

0

आप विश्रामडिफ़ॉल्ट/मानक heightForHeaderInSection चाहते हैं, इस का उपयोग करें:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return 44; 
} 

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 44)]; 

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 11, tableView.frame.size.width-100, 21)]; 

    label.text = @"Text"; 
    label.textColor = [UIColor colorWithRed:76.0/255 green:86.0/255 blue:108.0/255 alpha:1]; 
    label.backgroundColor = [UIColor clearColor]; 
    label.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.0]; 

    [view addSubview:label]; 

    return view; 
} 
संबंधित मुद्दे