2012-10-01 17 views
12

पर डिफ़ॉल्ट के बजाय UITableView अनुभाग शीर्षलेख पर क्लिक करें मैं एक ऐप पर काम कर रहा हूं जहां यह आवश्यक है कि अनुभाग का शीर्षलेख डिफ़ॉल्ट बाईं ओर दाईं ओर होना चाहिए।बाईं ओर

मैं खोज की है और कई के शौकीनों को लागू करने का सुझाव दिया:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    static UIView *headerView; 

    if (headerView != nil) 
     return headerView; 

    NSString *headerText = NSLocalizedString(@"البحث الأخيرة", nil); 

    // set the container width to a known value so that we can center a label in it 
    // it will get resized by the tableview since we set autoresizeflags 
    float headerWidth = 150.0f; 
    float padding = 10.0f; // an arbitrary amount to center the label in the container 

    headerView = [[UIView alloc] initWithFrame:CGRectMake(300, 0.0f, headerWidth, 44.0f)]; 
    headerView.autoresizingMask = UIViewAutoresizingFlexibleWidth; 

    // create the label centered in the container, then set the appropriate autoresize mask 
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(padding, 0, headerWidth - 2.0f * padding, 44.0f)]; 
    headerLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 
    headerLabel.textAlignment = UITextAlignmentRight; 
    headerLabel.text = headerText; 

    [headerView addSubview:headerLabel]; 

    return headerView; 
} 

हालांकि, जब मैं बढ़ाने की कोशिश हैडर दृश्य फ्रेम का x- निर्देशांक, यह देखने की स्थिति प्रभावित नहीं करता। और यह हमेशा तालिका दृश्य के केंद्र में है।

मुझे दाईं ओर होने के लिए शीर्षलेख की आवश्यकता है। सब से

+0

बजाय एक flexibleWidth के साथ जा रहा है, तो आप पूरी चौड़ाई होने के लिए यह मजबूर नहीं कर सकते हैं? फिर दृश्य पूर्ण चौड़ाई का उपभोग करेगा और पाठ को सही ठहराएगा। – bryanmac

+0

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

+0

@ डेविड आप सही हैं। रिक्त स्थान जोड़ने का विचार अच्छा नहीं है। हालांकि यह मेरे लिए समय के लिए काम किया।जैसा कि मैंने अपने प्रश्न में उल्लेख किया था कि दृश्य हमेशा केंद्र पर हो रहा था, जहां मुझे इसे सही होने की आवश्यकता है। फ्रेम सेट करना काम नहीं कर रहा था। इसके अलावा लेबल दाएं गठबंधन था। मैं समझता हूं कि UIView का उपयोग करना अधिक अनुकूलन विकल्प रखने के लिए बहुत बेहतर है। टिप्पणी के लिए धन्यवाद! –

उत्तर

15

यह मेरे लिए काम किया, फिर भी अपनी आवश्यकताओं के अनुसार संरेखित करने के लिए फ्रेम के समन्वय के साथ खेलते हैं

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UILabel *label = [[UILabel alloc] init]; 
    [email protected]"header title"; 
    label.backgroundColor=[UIColor clearColor]; 
    label.textAlignment = NSTextAlignmentRight; 
    return label; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return 50; 
} 
+1

हाँ यह मानते हुए सही है कि UILabel Uiview –

+1

का उप-वर्ग है धन्यवाद! इसने काम कर दिया :)। हालांकि, पाठ की शुरुआत लगभग सीमा सीमा को छू रही थी। मैंने पाठ में कुछ रिक्त स्थान जोड़े और अब पाठ बाईं ओर थोड़ा सा झुका हुआ है। –

+3

'UITextAlignmentRight' को बहिष्कृत किया गया है, 'NSTextAlignmentRight' –

0

firt मैं अपने मूल x निर्देशांक 300.For शीर्षक और footerviews सेट अपने मूल CGPoint (0.0) के लिए समन्वय और सिर्फ आकार के साथ खेलने के लिए किया जाएगा के बाद से

headerView = [[UIView alloc] initWithFrame:CGRectMake(300, 0.0f, headerWidth, 44.0f)]; 

साथ UIView init नहीं होता।

लेबल को स्वयं को देखने के रूप में बड़ा बनाने की कोशिश करें और इसके संरेखण को सही पर सेट करें।

UIView *headerTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)]; 

UILabel *sectionTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, headerTitleView.frame.size.width, 40)]; 
sectionTitleLabel.backgroundColor = [UIColor clearColor]; 
sectionTitleLabel.textAlignment = UITextAlignmentright; 

एक और संभावना मध्य संरेखण के साथ शीर्ष लेख दृश्य के दाईं ओर कहीं एक UILabel जोड़ है।

0

मुझे यकीन है कि मैं इस स्थिति को समझ में नहीं हूँ, लेकिन मैं यह सोचते कर रहा हूँ कि आप बस चाहते हैं बाएं के बजाए दाएं गठबंधन करने के लिए लेबल।

आपके हेडर व्यू के लिए फ्रेम मान समायोजित करना काम नहीं करेगा क्योंकि tableView इसे स्थितिबद्ध करने के लिए ज़िम्मेदार है और यह आपके द्वारा सेट किए गए किसी भी मान को अनदेखा कर देगा।

आपका एकमात्र विकल्प हेडर व्यू के सबव्यूज़ के साथ काम करना है।

उदाहरण के लिए, अपने headerLabel स्थिति की स्थापना सही करने के लिए गठबंधन किया जाना:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    static UIView *headerView; 

    if (headerView != nil) 
     return headerView; 

    NSString *headerText = NSLocalizedString(@"البحث الأخيرة", nil); 

    float headerWidth = 320.0f; 
    float padding = 10.0f; 

    headerView = [[UIView alloc] initWithFrame:CGRectMake(300, 0.0f, headerWidth, 44.0f)]; 
    headerView.autoresizesSubviews = YES; 

    // create the label centered in the container, then set the appropriate autoresize mask 
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, headerWidth, 44.0f)]; 
    headerLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth; 
    headerLabel.textAlignment = UITextAlignmentRight; 
    headerLabel.text = headerText; 

    CGSize textSize = [headerText sizeWithFont:headerLabel.font constrainedToSize:headerLabel.frame.size lineBreakMode:UILineBreakModeWordWrap]; 
    headerLabel.frame = CGRectMake(headerWidth - (textSize.width + padding), headerLabel.frame.origin.y, textSize.width, headerLabel.frame.size.height); 

    [headerView addSubview:headerLabel]; 

    return headerView; 
} 
+0

का उपयोग करना चाहिए संरेखण पहले से ही सही पर सेट है। –

7

मैं मुसीबत ऊपर समाधान के साथ पाठ बदलने के लिए किया था, इस के अनुगामी किनारे से उचित दूरी के साथ मुझे और स्थानों के लिए काम किया टेबल व्यू स्विफ्ट

UITableViewDataSource

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return 30 
} 

UITAbleViewDelegate में पी एस समाधान

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return "Header Title" 
} 

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 

    let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView 
    header.textLabel?.font = UIFont(name: "AvenirNext-Regular", size: 14.0) 
    header.textLabel?.textAlignment = NSTextAlignment.Right 

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