2013-07-17 6 views
6

में दो अलग-अलग कस्टम सेल को कॉल करना मैंने एक कस्टम सेल फ़ीचरसेल बनाया है जिसमें पंक्ति में 5 छवियां हैं जिन्हें मुख्य दृश्य में बुलाया जाएगा, लेकिन जब मैं इसे कॉल करता हूं तो मुझे खाली पंक्ति मिलती है। तो कृपया मेरी समस्या कहां हो सकती है?एक UITableView समस्या

मैंने कस्टम सेल के बारे में गुमराह किया है और मैंने जिस तरह से नीचे दिए गए कोड में उपयोग करना है, लेकिन कुछ भी नहीं हुआ है।

यह मेरा FeatureCell.h

@interface FeatureCell : UITableViewCell{ 

    IBOutlet UIImageView *img1; 
    IBOutlet UIImageView *img2; 
} 

@property (nonatomic, retain) UIImageView *img1; 
@property (nonatomic, retain) UIImageView *img2; 
@end 

है यह मेरा FeatureCell.m है

@implementation FeatureCell 

@synthesize img,img1,img2,img3,img4; 
@end 

यह मेरा दृश्य-नियंत्रक है .m

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{ 

return 2; 
} 

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

if (section == 0) { 
    return [objectCollection count]; 
} 
else{ 
    return 1; 
    } 
}  

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

static NSString* cellIdentifier1 = @"FeatureCell"; 

FeatureCell *cell1 = (FeatureCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1]; 


if (cell1 == nil) 
{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIdentifier1 owner:nil options:nil]; 

    cell1 = (FeatureCell*)[nib objectAtIndex:0]; 
} 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 
            reuseIdentifier:CellIdentifier]; 

} 


if ([indexPath section] == 0) { 

    containerObject = [objectCollection objectAtIndex:indexPath.row]; 

    [[cell textLabel] setText:containerObject.store]; 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

} 
    else{ 

    cell1.img1.image = [UIImage imageNamed:@"shower.png"]; 
    cell1.img2.image = [UIImage imageNamed:@"parking.png"]; 

    } 
     return cell; 
} 
+0

आप दुकानों से कनेक्ट किया था? – Wain

+0

यह 2 अलग-अलग सेल प्रकारों से निपटने का सही तरीका नहीं है। मेरा उत्तर यहां देखें: http://stackoverflow.com/questions/17711452/change-uicollectionviewcell-content-and-nib-layout-based-on-data/17711644#17711644। यह संग्रह दृश्यों से संबंधित है, लेकिन यह तालिका दृश्यों के साथ समान है। – rdelmar

+0

@Wain आउटलेट द्वारा आपका क्या मतलब है? –

उत्तर

13

आप कुछ इस तरह करने की जरूरत है:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.section == 0) { 
     static NSString *CellIdentifier = @"Cell"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 
           reuseIdentifier:CellIdentifier]; 
     } 

     containerObject = [objectCollection objectAtIndex:indexPath.row]; 
     [[cell textLabel] setText:containerObject.store]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     return cell; 
    } else { 
     static NSString* cellIdentifier1 = @"FeatureCell"; 

     FeatureCell *cell1 = (FeatureCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1]; 
     if (cell1 == nil) { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIdentifier1 owner:nil options:nil]; 
      cell1 = (FeatureCell*)[nib objectAtIndex:0]; 
     } 

     cell1.img1.image = [UIImage imageNamed:@"shower.png"]; 
     cell1.img2.image = [UIImage imageNamed:@"parking.png"]; 

     return cell1; 
    } 
} 

मैं if हालत पीछे की ओर तो जांच कर लें कि हो सकता है।

+0

cellForRowAtIndexPath को UITableViewCell उदाहरण वापस करने की आवश्यकता है। आपका कोड एक त्रुटि फेंक देगा, क्योंकि आप if-else कथन के बाहर कुछ भी वापस नहीं कर रहे हैं। – Sebyddd

+0

@ सेबीडड नहीं, यह ठीक है। – rmaddy

+0

बस कोशिश की, 'नियंत्रण गैर-शून्य कार्य के अंत तक पहुंच सकता है।' :) – Sebyddd

0

इस कार्यान्वयन:

@implementation FeatureCell 
@synthesize img,img1,img2,img3,img4; 
@end 

कुछ एक्सेसर विधियों वाले सेल में परिणाम लेकिन प्रारंभिक उदाहरण नहीं। उदाहरणों को बनाने या कनेक्ट करने के लिए आपको init विधि को लागू करने की आवश्यकता है (जिन्हें आप आउटलेट के रूप में परिभाषित करते हैं) संबंधित XIB फ़ाइल में कनेक्ट करें। this guide पढ़ें।

मैं आपके विभिन्न सेल उदाहरणों को बनाने के तरीके के बारे में टिप्पणियों के बारे में टिप्पणियों से भी सहमत हूं। यह बहुत असंगठित है और आप संदर्भों को मिश्रण कर रहे हैं। आपको चीजों को अलग-अलग तरीकों से विभाजित करना चाहिए या कम से कम if कथन के अंदर कक्ष बनाने के लिए कोड डालना चाहिए ताकि आप सेल इंस्टेंस नाम टाइप न कर सकें।

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

{ अगर (indexPath.row == 0) // नहीं indexPath.section ... मेरी समस्या का समाधान होने

{ 
    static NSString *CellIdentifier = @"MenuHeadingCustomCell"; 

    MenuHeadingCustomCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell ==nil) 
    { 
     cell = [[MenuHeadingCustomCell alloc]initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:CellIdentifier]; 
    } 

    return cell; 
} 





else 
    { 
     static NSString* cellIdentifier1 = @"LevelViewCell"; 
     LevelViewCell *cell1 =[tableView dequeueReusableCellWithIdentifier:cellIdentifier1]; 
     if (cell1 ==nil) 
     { 
      cell1 = [[LevelViewCell alloc]initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:cellIdentifier1]; 

     // row counts which dictionary entry to load: 
     _Dictionary=[_array objectAtIndex:indexPath.row]; 

     cell1.LevelTitleText.text =[NSString stringWithFormat:@"%@",[_Dictionary objectForKey:@"LevelLabelText"]]; 
     cell1.LevelSubText.text =[NSString stringWithFormat:@"%@",[_Dictionary objectForKey:@"LevelLabelSubText"]]; 
     cell1.LevelThumb.image =[UIImage imageNamed:[_Dictionary objectForKey:@"LevelLabelThumb"]]; 

    } 
    return cell1; 

}