2013-02-19 13 views
19

तो, ऐसा लगता है कि heightForRowAtIndexPath को cellForRowAtIndexPath से पहले बुलाया जाता है। मैं वर्तमान में cellForRowAtIndexPath उपयोग कर रहा हूँ प्रत्येक कक्ष की ऊंचाई (वे चर ऊंचाइयों कर रहे हैं, उस में पाठ की मात्रा के साथ एक UILabel का उपयोग कर की गणना के लिए।कॉलिंग ऊंचाई ForRowAtIndexPath CellForRowAtIndexPath के भीतर से?

मैं बिल्कुल के रूप में हैरान है कि कैसे मैं सेल की ऊंचाई को ठीक से सेट कर सकते हैं अगर विधि है कि सेट ऊंचाई विधि से पहले कहा जाता है कि की गणना करता है यह।

मैं NSString की एक श्रेणी बनाई है, और UILabel है, जो एक साथ काम करने का एक और UILabel सही आकार बनाते हैं। मुद्दा यह है कि UITableViewCell उदाहरण जिनमें वे लेबल शामिल हैं, का आकार बदल नहीं रहा है, इसलिए प्रत्येक लेबल के अंत में लेबल लटकते हैं, नीचे वाले लोगों को ओवरलैप करते हैं।

मुझे पता है कि मुझे क्या करना है, लेबल की ऊंचाई के बराबर सेल की ऊंचाई, साथ ही कुछ अतिरिक्त मार्जिन स्पेस भी है, लेकिन मैं इस बात से परेशान हूं कि लेबल ऊंचाई से पहले मैं ऊंचाई कैसे निर्धारित कर सकता हूं गणना की गई। क्या मैं ऊंचाई को heightForRowAtIndexPath का उपयोग किए बिना सेट कर सकता हूं? या यदि नहीं, तो क्या मैं कहीं और सेल की ऊंचाई की गणना कर सकता हूं? मैं वर्तमान में ऐसा करने के लिए indexPath.row का उपयोग कर रहा हूं, इसलिए इसे पहले से करना मुश्किल होगा। यहाँ कोड मेरी cellForRowAtIndexPath है:

cellForRowAtIndexPath

- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath 
{ 
    FQCustomCell *_customCell = [tableView dequeueReusableCellWithIdentifier: @"CustomCell"]; 
    if (_customCell == nil) { 
     _customCell = [[FQCustomCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"CustomCell"]; 
    } 

    _customCell.myLabel.text = [[_loadedNames objectAtIndex: indexPath.row] name]; 
    _customCell.myLabel.font = [UIFont fontWithName: @"FontA" size: 15.0f]; 

    UIView *backView = [[UIView alloc] initWithFrame: CGRectZero]; 
    backView.backgroundColor = [UIColor clearColor]; 
    _customCell.backgroundView = backView; 

    _customCell.myLabel.frame = CGRectMake(5, 5, 265, 65); 

    [_customCell.myLabel sizeToFitMultipleLines]; 
    return _customCell; 
} 
+0

आईओएस 8 से शुरू करना आप स्वचालित रूप से गणना की गई गतिशील पंक्ति ऊंचाई का भी उपयोग कर सकते हैं। http: // stackoverflow।कॉम/प्रश्न/18746929/उपयोग-ऑटो-लेआउट-इन-यूटेबलव्यू-टू-डायनामिक-सेल-लेआउट-वेरिएबल-पंक्ति-ऊंचाई – Suragch

उत्तर

15

आप आप लेबल आकार पर कुछ बाधाओं सेट कर सकते हैं, heightForRowAtIndexPath में सेल की ऊंचाई की गणना करनी है की तरह

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *theText=[[_loadedNames objectAtIndex: indexPath.row] name]; 
    CGSize labelSize = [theText sizeWithFont:[UIFont fontWithName: @"FontA" size: 15.0f] constrainedToSize:kLabelFrameMaxSize]; 
    return kHeightWithoutLabel+labelSize.height; 
} 

कुछ kLabelFrameMaxSize

#define kLabelFrameMaxSize CGSizeMake(265.0, 200.0) 
सेट करके

फिर आप परिवर्तनीय लेबल ऊंचाई के साथ स्थिर ऊंचाई जोड़कर ऊंचाई वापस कर देते हैं।

इसके अलावा, एक समान होना चाहिए, आप एक ही पद्धति के बजाय sizeToFitMultipleLines

- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath 
{ 
    FQCustomCell *_customCell = [tableView dequeueReusableCellWithIdentifier: @"CustomCell"]; 
    if (_customCell == nil) { 
     _customCell = [[FQCustomCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"CustomCell"]; 
    } 

    _customCell.myLabel.text = [[_loadedNames objectAtIndex: indexPath.row] name]; 
    _customCell.myLabel.font = [UIFont fontWithName: @"FontA" size: 15.0f]; 

    UIView *backView = [[UIView alloc] initWithFrame: CGRectZero]; 
    backView.backgroundColor = [UIColor clearColor]; 
    _customCell.backgroundView = backView; 

    CGSize labelSize = [_customCell.myLabel.text sizeWithFont:_customCell.myLabel.font constrainedToSize:kLabelFrameMaxSize]; 
    _customCell.myLabel.frame = CGRectMake(5, 5, labelSize.width, labelSize.height); 

    return _customCell; 
} 
+0

बिल्कुल सही, धन्यवाद! :) – Luke

-1

का उपयोग कर हर कोशिका heightforRowAtIndexPath के लिए पहले CellForRowAtIndexPath कहा जाता है और उसके बाद है cellForRowAtIndexPath में फ्रेम सेट करने के लिए उपयोग करना चाहिए। तो आप पहले ऊंचाई की गणना कर सकते हैं और फिर प्रत्येक सेल के लिए CellForRowAtIndexPath में डेटा भर सकते हैं।

+5

आपने अभी कहा है कि मैंने वास्तव में क्या कहा है .. – Luke

1

आपको heightForRowAtIndexPath विधि में सेल की ऊंचाई की गणना करनी चाहिए। यह जेपी Hribovsek के जवाब में heightForRowAtIndexPath कार्यान्वयन की तरह, किसी भी UILabel का उपयोग किए बिना किया जाना चाहिए।

फिर cellForRowAtIndexPath में किसी भी सबव्यूज़ के फ्रेम को सेट न करें और लेबल के sizeToFitMultipleLines विधि को कॉल न करें। इसके बजाय, layoutSubviewsFQCustomCell की विधि को कार्यान्वित करें जैसे कि सबव्यू फ़्रेम सेल की किसी भी संभावित सीमा के लिए सही ढंग से सेट किए गए हैं। आपको इस विधि में टेक्स्ट या फ़ॉन्ट आकार की परवाह नहीं करनी चाहिए, बस सुनिश्चित करें कि लेबल के मार्जिन सही हैं। यदि सेल की ऊंचाई heightForRowAtIndexPath विधि में सही ढंग से गणना की जाती है, तो आपके लेबल का आकार ठीक होगा।

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