2013-04-10 11 views
8

मेरे पास स्थैतिक कोशिकाओं वाला एक टेबल है। एक सेल के लिए मैं लेबल की ऊंचाई (उस सेल के अंदर) के आधार पर अपनी ऊंचाई बदलना चाहता हूं और साथ ही साथ अन्य सभी कोशिकाओं की ऊंचाई को बरकरार रखना चाहता हूं। मैं वर्तमान सेल की ऊंचाई कैसे प्राप्त कर सकता हूं? या शायद बेहतर दृष्टिकोण है?ऊंचाई के अंदर वर्तमान सेल ऊंचाई ForRowAtIndexPath?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if ([indexPath section] == 2) { 
     return self.myLabel.frame.origin.y *2 + self.myLabel.frame.size.height; 
    } else { 
     return ...; // what should go here, so the cell doesn't change its height? 
    } 
} 
+3

आपका सेल के डिफ़ॉल्ट ऊंचाई वहाँ चला जाता है। 44 अगर आपने कुछ भी नहीं बदला है। – Desdenova

+0

आप एनआईएसटीआईटी विधि 'आकार WithFont:' पर ​​UIKIt के एक्सटेंशन का उपयोग कर लेबल के लिए सेल की वास्तविक आवश्यक ऊंचाई की गणना भी कर सकते हैं। –

उत्तर

12

आप कॉल कर सकते हैं:

[super tableView:tableView heightForRowAtIndexPath:indexPath] 

किसी और ब्लॉक में तो तुम अगर कभी आप डिफ़ॉल्ट ऊंचाई बदल चिंता करने की जरूरत नहीं है।

4

आप/सेट डिफ़ॉल्ट ऊंचाई tableView.rowHeight प्राप्त कर सकते हैं, या आप, सेल ऊंचाई बदलने से पहले अपने ऊंचाई संगृहीत करेगा इसलिए आपको कुछ चर से डिफ़ॉल्ट ऊंचाई प्राप्त कर सकते हैं कर सकते हैं;

1

कृपया इस एक

if ([indexPath section] == 2) 
{ 
    if(indexPath.row == 1) 
     return self.myLabel.frame.origin.y *2 + self.myLabel.frame.size.height; 
    else 
     tableView.rowHeight 
} 
0

आप, हो सकता है, कंस्ट्रेन्ड चौड़ाई में लेबल की ऊंचाई की गणना करना चाहते हैं। इस मामले में आप इस तरह विधि बना सकते हैं:

- (CGFloat)textHeightOfMyLabelText { 
    CGFloat textHeight = [self.myLabel.text sizeWithFont:self.myLabel.font constrainedToSize:LABEL_MAX_SIZE lineBreakMode:self.myLabel.lineBreakMode].height; 
    return textHeight; 
} 

और - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath में परिणाम का उपयोग करें। अपने लेबल का मार्जिन मान जोड़ने के लिए मत भूलना।

+0

हाँ, thx, मैं बस इसे बदलना होगा =) – Ossir

0
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == youSectionNumber) 
    { 
     if (indexPath.row == numberOfrow) 
     { 
      return self.myLabel.frame.origin.y *2 + self.myLabel.frame.size.height; 
     } 
    } 

     return 44; // it is default height of cell; 
} 
1

@talnicolas महान जवाब, यहां स्विफ्ट 3 के लिए है:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    if indexPath.section == 2 { 
     let easy = self.myLabel.frame 
     return easy.origin.y *2 + easy.size.height 
    } else { 
     return super.tableView(tableView, heightForRowAt: indexPath) 
    } 
} 
संबंधित मुद्दे