2009-12-22 28 views
22

में UILabel चौड़ाई की गतिशील गणना मैं UITableViewCellStyle1 (या तो) के आधार पर एक कस्टम, इन-एप्लिकेशन सेटिंग्स दृश्य बना रहा हूं।UITableViewCell

मैं एक सेल के बाएं लेबल (शीर्षक) की चौड़ाई की गतिशील गणना करने की कोशिश कर रहा हूं यह निर्धारित करने के लिए कि दाईं ओर मेरा टेक्स्ट फ़ील्ड कितना चौड़ा हो सकता है।

जब मैं सिम्युलेटर और दृश्य लोड में ऐप लॉन्च करता हूं, तब तक शीर्षक लेबल की चौड़ाई शून्य होती है जब तक कि मैं दृश्य को स्क्रॉल नहीं करता और एक सेल दिखाई नहीं देता है, तब जब मैं बैक अप को स्क्रॉल करता हूं तो उम्मीद के अनुसार समायोजित होता है। मेरा मानना ​​है कि इसे सेल पुन: उपयोग के साथ कुछ करना पड़ सकता है।

मुझे दृश्य लोड होने पर कोशिकाओं में शीर्षक लेबल की आवश्यकता होती है, जब वे एक बार दृष्टि से बाहर नहीं निकलते हैं।

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath 
    { 
     static NSString* reuseIdentifier = @"SettingsCell"; 

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; 

if (!cell) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier] autorelease]; 
    [cell setBackgroundColor:[UIColor baeDarkGrayColor]]; 


    UILabel* cellLabel = [cell textLabel]; 
    [cellLabel setTextColor:[UIColor whiteColor]]; 
    [cellLabel setBackgroundColor:[UIColor clearColor]]; 

    [cell setUserInteractionEnabled:YES]; 
    [cell setSelectionStyle:UITableViewCellSelectionStyleGray]; 
} 

// Populate cell with corresponding data. 
NSDictionary* tableSection = [_tableData objectAtIndex:indexPath.section]; 

// Set the label 
UILabel* textLabel = [cell textLabel]; 
NSString* labelString = [[tableSection objectForKey:@"itemTitles"] objectAtIndex:indexPath.row]; 
[textLabel setText:labelString]; 

    // CGSize constrainedSize; 
    // constrainedSize.width = MAXFLOAT; 
// constrainedSize.height = MAXFLOAT; 

CGSize textLabelSize = [textLabel.text sizeWithFont:textLabel.font /*constrainedToSize:constrainedSize*/]; 

NSLog(@"text label width: %f", textLabelSize.width);  


// Set the accessory view 
BAESettingsTableCellAccessoryType accessoryType = [[[tableSection objectForKey:@"itemAccessories"] objectAtIndex:indexPath.row] integerValue]; 
switch (accessoryType) 
{ 
    case BAESettingsTableCellAccessoryDisclosureIndicator: 
    { 
     UIImageView* disclosureView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"AccessoryDisclosure.png"]]; 
     [cell setAccessoryView:disclosureView]; 
     [disclosureView release]; 
     break; 
    } 
    case BAESettingsTableCellAccessoryOnOffSwitch: 
    { 
     UISwitch* onOffSwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; 
     [onOffSwitch setOn:YES]; 
     [cell setAccessoryView:onOffSwitch]; 
     [onOffSwitch release]; 
     break; 
    } 
    case BAESettingsTableCellAccessoryNone: 
     // default: 
     // break; 
    { 

     CGRect cellFrame = [cell frame]; 

     float detailTextFieldWidth = cellFrame.size.width - (textLabelSize.width + 50); 
     float detailTextFieldHeight = cellFrame.size.height - 1; 

     NSLog(@"detail text field calculated frame width, height, %f, %f", detailTextFieldWidth, detailTextFieldHeight); 


     CGRect frame = CGRectMake(cellFrame.origin.x, cellFrame.origin.y, detailTextFieldWidth, detailTextFieldHeight); 


     UITextField* textField = [[UITextField alloc] initWithFrame:frame]; 
     NSString* detailLabelString = [[tableSection objectForKey:@"itemDetailStrings"] objectAtIndex:indexPath.row]; 
     textField.text = detailLabelString; 
     textField.textColor = cell.detailTextLabel.textColor; 
     textField.textAlignment = UITextAlignmentRight; 
     textField.borderStyle = UITextBorderStyleRoundedRect; 
     [cell setAccessoryView:textField]; 
     [textField release]; 
     break; 
    } 

} 

return cell; 
    } 

उत्तर

16

स्पष्ट रूप से फ़ॉन्ट को सेट करने से आपकी समस्या हल हो जाती है।

CGSize textLabelSize = [textLabel.text sizeWithFont:[UIFont systemFontOfSize:17]]; 

Gory detals:
tableview पहली बार लोड, यह स्निपेट फ़ॉन्ट आकार जा रहा है 0.

[textLabel.text sizeWithFont:textLabel.font] 

मैं इस के बिंदु आकार प्रदर्शित करके पता लगा होने के कारण शून्य चौड़ाई लौटाता है सेल फ़ॉन्ट

NSLog(@"font size: %f", cell.textLabel.font.pointSize); 

बिंदु आकार तब तक शून्य था जब तक कि सेल स्क्रीन बंद नहीं हो गया और जैसा आपने वर्णन किया था, वापस आ गया।

3

मुझे एक ही समस्या मिली, लेकिन मुझे एक कामकाज मिला है। एफवाईआई मैंने ऐप्पल (बग आईडी # 7535066) के साथ एक बग उठाया है। लेक्स, आपके उदाहरण में, यदि आप textLabel.font के बजाय cell.font का उपयोग करते हैं, तो यह काम करना चाहिए, हालांकि आपको एक कंपाइलर चेतावनी मिलेगी, क्योंकि यह एक बहिष्कृत विधि कॉल है। मुझे यकीन नहीं है कि क्या आप उठाए गए बग के विवरण देख पाएंगे, इसलिए मैं यहां विवरणों को चिपका रहा हूं:

आईफोन एसडीके 3.1.2 (7 डी 11) एनएसएसटींग यूआईकिट अतिरिक्त विधि विधि आकार के साथ रिटर्न शून्य

सारांश मैं UITableView प्रतिनिधि विधि में UITableViewCells सजाने कर रहा हूँ:

  • (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath

मैं पहले से ही सेल में पाठ की लंबाई को खोजने के लिए, सही ढंग से नए दृश्य को स्वरूपित करना चाहते हैं, तो मैं NSString UIKit परिवर्धन विधि बोल रहा हूँ:

  • (CGSize) sizeWithFont: (UIFont *) फ़ॉन्ट constrainedToSize: (CGSize) आकार

जब मैं फ़ॉन्ट तर्क के लिए cell.textLabel.font उपयोग करें, यह हमेशा एक खाली CGSize रिटर्न

अगर मैं तर्क के लिए बहिष्कृत किया गया विधि, cell.font उपयोग करें, यह CGSize वापसी करता है अपेक्षित मूल्य।

1 को पुन: उत्पन्न करने के लिए चरण 1।एक तालिका दृश्य के साथ एक UITableViewController बनाएं, और इसे कुछ नमूना डेटा के साथ पॉप्युलेट करें 2. स्टब किए गए विधि में सेलफॉररोएटइंडेक्सपैथ, टिप्पणी के बाद // सेल सेट करें, और जहां cell.textLabel.text पॉप्युलेट किया गया है, निम्न को रखें:


CGSize size = [cell.textLabel.text sizeWithFont:[[cell textLabel] font] constrainedToSize:CGSizeMake(320, 480)]; 
NSLog(@"cell frame: %f %f", size.width, size.height); 

CGSize size2 = [cell.textLabel.text sizeWithFont:[cell font] constrainedToSize:CGSizeMake(320, 480)]; 
NSLog(@"DEPRECATED cell frame: %f %f", size2.width, size2.height); 

उम्मीद परिणामों

कुछ भी है, तो आप कॉल फॉन्ट को पुनः प्राप्त करने पदावनत विधि का उपयोग करता है उम्मीद करेंगे, तो विफल, या कम से कम दोनों एक ही परिणाम वापस जाने के लिए।

वास्तविक परिणाम:

2010-01-12 22: 06: 36.645 PrefTest [9659: 207] सेल फ्रेम: ०.०००००० ०.०००००० 2010-01-12 22: 06: 36.646 PrefTest [9659: 207] हटाया गया सेल फ्रेम: 88.000000 24.000000

कारगर युक्तियाँ & अलगाव:

के रूप में बताया गया है, पदावनत विधि का उपयोग फ़ॉन्ट मिलता है। लेकिन यह अजनबी हो जाता है!


CGSize size2 = [cell.textLabel.text sizeWithFont:[cell font] constrainedToSize:CGSizeMake(320, 480)]; 
NSLog(@"DEPRECATED cell frame: %f %f", size2.width, size2.height); 

    CGSize size = [cell.textLabel.text sizeWithFont:[[cell textLabel] font] constrainedToSize:CGSizeMake(320, 480)]; 
NSLog(@"cell frame: %f %f", size.width, size.height); 

परिणाम अब कर रहे हैं:

2010-01-12 22: 06: 36.645 PrefTest [9659: 207] सेल फ्रेम: 88,000000 24,000000

दो तरीकों मैं ऊपर दिया रिवर्स 2010-01-12 22: 06: 36.646 प्रीफटेस्ट [965 9: 207] डिस्प्लेटेड सेल फ्रेम: 88.000000 24.000000

ऐसा लगता है कि [सेल फ़ॉन्ट] संदर्भित करने के लिए [सेल फ़ॉन्ट लेबल] फ़ॉन्ट को मजबूर करने के लिए पर्याप्त है [सेल सेल लेबल] फ़ॉन्ट]।

5

मैं पूरी तरह से सहमत हूं कि यह पर्याप्त रूप से परेशान है कि यह एक बग होना चाहिए। सेल को पूरी तरह से स्थापित करने के बिना ऐप्पल में अच्छे लोग कभी भी सेल ऊंचाई के लिए क्यों पूछेंगे। बेवकूफ! हालांकि, एक पदावनत समारोह उपयोग करने के बजाय, की कोशिश:

[cell layoutIfNeeded]; 

इस विधि कॉलिंग सेल सीमा, सेल फ़ॉन्ट, आदि इसलिए, इस के बाद किसी भी गणना की पहुंच है सहित स्थापित किया जा रहा के साथ एक ही प्रभाव पड़ता है पूरी तरह से सेल स्थापित करें। यहाँ पूरे कोड है:

-(CGFloat) tableView:(UITableView *) tableView heightForRowAtIndexPath:(NSIndexPath *) indexPath { 

    // Find the cell for this index path 
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; 
    CGFloat cellHeight = cell.frame.size.height; 

    // Calculate text size after forcing a layout 
    [cell layoutIfNeeded]; 
    CGSize textSize = [cell.textLabel.text sizeWithFont:cell.textLabel.font constrainedToSize:CGSizeMake(cell.contentView.bounds.size.width, 99999) lineBreakMode:cell.textLabel.lineBreakMode]; 

    // Only change the cell height if the text forces a growth 
    if (textSize.height > cellHeight) { 
    cellHeight = textSize.height; 
    } 

    return cellHeight; 
} 
+0

यह काम नहीं करता – Dejell

+0

अद्भुत विचार। [सेल लेआउट IfNeeded]; बहुत अच्छा काम करता है! अन्य समाधान काम नहीं करते हैं। आपका बहुत बहुत धन्यवाद! – Vyacheslav

3

उपयोग [myLbl sizeToFit]; लेबल को स्वचालित रूप से आकार देने के लिए

यहां नमूना कोड है।

UILabel *lbl1, *lbl2; 
    if (cell == nil) { 

    UILabel *lbl1 = [[UILabel alloc] init]; 
      [lbl1 setFont:[UIFont boldSystemFontOfSize:20]]; 
      [cell.contentView addSubview:lbl1]; 
      lbl1.frame = CGRectMake(3, 10, 0, 0); 
      lbl1.tag = 10; 
      [lbl1 release]; 

     UILabel *lbl2 = [[UILabel alloc] init]; 
     [lbl2 setFont:[UIFont systemFontOfSize:20]]; 
     [cell.contentView addSubview:lbl2];     
     lbl2.tag = 20; 
     [lbl2 release]; 
} 
else { 
     lbl1 = (UILabel*)[cell.contentView viewWithTag:10]; 
     lbl2 = (UILabel*)[cell.contentView viewWithTag:20]; 
} 

    lbl1.text = @"Hello "; 
    [lbl1 sizeToFit]; 

lbl2.text = @"World"; 
     [lbl2 sizeToFit]; 
     lbl2.frame = CGRectMake(lbl1.frame.origin.x+lbl1.frame.size.width+5, 
           lbl1.frame.origin.y, 
           lbl2.frame.size.width, 
           lbl1.frame.size.height);