2012-05-25 15 views
8

के साथ UITableView पर कक्षों के बीच अंतर मेरे पास एक XIB फ़ाइल से एक कस्टम UITableViewCell लोड करने वाला UITableView है। सब ठीक काम कर रहा है, लेकिन मेरे लेआउट की आवश्यकता है कि कोशिकाओं (सभी एक ही खंड के अंदर) उनके बीच अंतर हो। कोई भी मौका यह पंक्ति ऊंचाई बढ़ाने के बिना किया जा सकता है?कस्टम UITableViewCell

कैसे यह अब
how it is now

है कि यह कैसे
how it's supossed to be

संपादित होने के लिए supossed है:
यह कैसे कोड है आज

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [[self.cards valueForKeyPath:@"cards"] count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    [ccTableView setBackgroundColor:[UIColor clearColor]]; 
    cardsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cardsCell"]; 

    if(cell == nil){ 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"cardsCell" owner:self options:nil]; 
     cell = [topLevelObjects objectAtIndex:0]; 
    } 

    NSString *nmCard = [[self.cards valueForKeyPath:@"cards.name"] objectAtIndex:indexPath.row]; 
    cell.descCardLabel.text = nmCard; 

    return cell; 
} 
+0

आप पंक्ति ऊंचाई क्यों नहीं बढ़ा सकते हैं? सबसे आसान मामले में –

+0

@obuseme एक xib और तीन पंक्ति राज्य (मध्य, शीर्ष, नीचे) है, कोड काफी उलझन में होगा। –

+0

@obuseme इसके अलावा, मेरे सेल में 35 ऊंचाई वाली पृष्ठभूमि छवि है जहां लेबल है। यदि मैं पंक्ति ऊंचाई बढ़ाता हूं, तो लेआउट यह होगा कि मैं कैसे चाहता हूं, लेकिन टैपिंग चयन छवि के नीचे, दूरी पर काम करेगा, और मैं गलत पंक्ति के आकस्मिक चयन नहीं चाहता हूं। – gmogames

उत्तर

7

आप कर सकते हैं, तो सेल की ऊंचाई बदल नहीं है, समाधान आवश्यक ऊंचाई की अदृश्य इंटरमीडिएट कोशिकाओं का उपयोग करना है। आपको उस मामले में टेबल व्यू प्रतिनिधि और डेटासोर्स पर इंडेक्स को पुन: गणना करने की आवश्यकता होगी।

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    static NSString *CELL_ID2 = @"SOME_STUPID_ID2"; 
    // even rows will be invisible 
    if (indexPath.row % 2 == 1) 
    { 
     UITableViewCell * cell2 = [tableView dequeueReusableCellWithIdentifier:CELL_ID2]; 

     if (cell2 == nil) 
     { 
      cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
              reuseIdentifier:CELL_ID2]; 
      [cell2.contentView setAlpha:0]; 
      [cell2 setUserInteractionEnabled:NO]; // prevent selection and other stuff 
     } 
     return cell2; 
    } 

    [ccTableView setBackgroundColor:[UIColor clearColor]]; 
    cardsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cardsCell"]; 

    if(cell == nil){ 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"cardsCell" owner:self options:nil]; 
     cell = [topLevelObjects objectAtIndex:0]; 
    } 

    // Use indexPath.row/2 instead of indexPath.row for the visible section to get the correct datasource index (number of rows is increased to add the invisible rows) 
     NSString *nmCard = [[self.cards valueForKeyPath:@"cards.name"] objectAtIndex:(indexPath.row/2)]; 
     cell.descCardLabel.text = nmCard; 

     return cell; 
    } 



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

    // two times minus one (invisible at even rows => visibleCount == invisibleCount+1) 
    return [[self.cards valueForKeyPath:@"cards"] count] * 2 - 1; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row % 2 == 1) 
     return 40; 
    return 162; 
} 

तुम भी :didSelectRowAtIndexPath: और अन्य तरीकों जहां यह प्रयोग किया जाता है के लिए indexPath.row पुनर्गणना के लिए की आवश्यकता होगी।

+0

इसे पूरा करने के तरीके पर कोड उदाहरण के लिए कोई मौका? मैंने प्रश्न में अपना वर्तमान कोड जोड़ा। धन्यवाद। – gmogames

+0

@Guilherme Mogames अद्यतन –

+1

देखें वास्तव में, यह केवल कोड सभी पंक्तियों को रिक्त बना देता है। उदाहरण के लिए, पहली पंक्ति (सेल 1) 0 लौट रही है, इसलिए पाठ को सेल के साथ वापस करने की बजाय, यह सेल 2 रिक्त वापस कर रहा है। तो मुझे टेबल में 2 आइटम मिलते हैं लेकिन पहला खाली है। – gmogames

4

हालांकि @ ए-लाइव तकनीकी रूप से सही है, ताकि अन्य मुद्दों, पूर्व को रोकने के लिए है:

: यदि आप एक indexPath.row/2 कहीं डाल करने के लिए भूल जाते हैं, तो आपको निम्न (जो किसी प्रोग्रामिंग शामिल है) कर सकते हैं उदाहरण के लिए कहें कि आपकी UITableViewCell ऊंचाई सामान्यतः 9 0 अंक है और आप प्रत्येक सेल के बीच 10 बिंदु अंतर चाहते हैं। अपने UITableViewCell को 100 अंक ऊंचा बनाएं और केवल UITableViewCell खाली के नीचे 10 अंक बनाएं (किसी भी प्रकार की कोई वस्तु नहीं)। फिर इंटरफ़ेस बिल्डर में UITableViewCell पर क्लिक करें और पृष्ठभूमि को सेट करें जो आप स्पेसिंग क्षेत्र के रंग को चाहते हैं।

वोला! आप प्रत्येक सेल के बीच एक छोटे से काम के साथ अंतर प्राप्त कर रहे हैं जो प्रोग्रामिंग/2 हर जगह से कहीं अधिक आसान है!

+0

मैं कुछ भी के साथ नीचे 10 कैसे बना सकता हूँ? मैंने बस सेल की पृष्ठभूमि को सफेद – Dejell

36

वास्तव में यह एक बहुत ही आसान समाधान है जो मैंने पाया, यदि आप कस्टम यूटेबलव्यूसेल कक्षाओं का उपयोग कर रहे हैं।

सेल वर्ग में, इस कोड जोड़ें:।

- (void)setFrame:(CGRect)frame { 
    frame.origin.y += 4; 
    frame.size.height -= 2 * 4; 
    [super setFrame:frame]; 
} 

यह आपको सेल ऊंचाई आप uitablview वर्ग आप में इस सेल फोन कर रहे हैं में डाल के भीतर एक 4pt बफर दे देंगे जाहिर है, अब आप के लिए है लेबल और छवियों को डालने पर सेल में उस कम जगह की क्षतिपूर्ति करें, लेकिन यह काम करता है। सेल की चौड़ाई को छोटा बनाने के लिए आप एक्स-अक्ष पर भी वही काम कर सकते हैं। मैंने अपने ऐप में अपनी कोशिकाओं के पीछे पृष्ठभूमि छवियों को दिखाने के लिए यह किया है।

+0

कूल पर सेट किया है! यह मेरी अपेक्षा से पूरी तरह से काम करता है! –

+0

कूल! इस समाधान के लिए वोट दें। – lee

+0

धन्यवाद, यही वह चीज़ है जिसे मैं ढूंढ रहा हूं :) – Harshad

0

यदि आप तेजी से समाधान की तलाश में हैं, तो reddit से यह उत्तर मेरे लिए पूरी तरह से काम करता है।

class CustomTableViewCell: UITableViewCell { 
override var frame: CGRect { 
    get { 
     return super.frame 
    } 
    set (newFrame) { 
     var frame = newFrame 
     frame.origin.y += 4 
     frame.size.height -= 2 * 4 
     super.frame = frame 
    } 
} 
}