2013-07-17 24 views
6

Utworzono niestandardową komórkę FeatureCell, która ma 5 obrazów w wierszu, który zostanie wywołany w widoku głównym, ale gdy go wywołasz, otrzymam pusty wiersz. Więc proszę, gdzie może być mój problem?Wywołanie dwóch różnych komórek niestandardowych w jednym wydaniu UITableView

Mam google o niestandardowej komórce i użyłem sposobu, który muszę użyć w poniższym kodzie, ale nic się nie dzieje.

To mój FeatureCell.h

@interface FeatureCell : UITableViewCell{ 

    IBOutlet UIImageView *img1; 
    IBOutlet UIImageView *img2; 
} 

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

To mój FeatureCell.m

@implementation FeatureCell 

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

To jest mój widok-kontroler .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

Czy połączyć placówki? – Wain

+0

To nie jest właściwy sposób postępowania z 2 różnymi typami komórek. Zobacz moją odpowiedź tutaj: http://stackoverflow.com/questions/17711452/change-uicollectionviewcell-content-and-nib-layout-based-on-data/17711644#17711644. Dotyczy to widoków kolekcji, ale jest tak samo w przypadku widoków tabel. – rdelmar

+0

@Wain co masz na myśli przez outlety? –

Odpowiedz

13

Trzeba zrobić coś takiego:

- (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; 
    } 
} 

mogę mieć kondycję if tyłu więc sprawdzić, czy podwójne.

+0

cellForRowAtIndexPath musi zwrócić wystąpienie UITableViewCell. Twój kod wygeneruje błąd, ponieważ nie zwracasz niczego poza instrukcją if-else. – Sebyddd

+0

@Sebyddd nie, wszystko w porządku. – rmaddy

+0

Po prostu wypróbowałem: "Kontrola może osiągnąć koniec funkcji nieważkości". :) – Sebyddd

0

Ta implementacja:

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

Wyniki w komórce z niektórymi metodami dostępowymi, ale bez inicjalizowanych wystąpień. Należy zastosować metodę init, aby utworzyć instancje lub połączyć zmienne (zdefiniowane jako punkty sprzedaży) z powiązanym plikiem XIB. Przeczytaj this guide.

Zgadzam się również z komentarzami dotyczącymi struktury tworzenia różnych instancji komórek. Jest bardzo zdezorganizowany i wydajesz się mieszać odniesienia. Powinieneś podzielić rzeczy na różne metody lub przynajmniej umieścić kod do tworzenia komórek wewnątrz instrukcji if, aby nie można było literować nazw instancji komórek.

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

{ if (indexPath.row == 0) // nie indexPath.section ... ustalone mój problem

{ 
    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; 

} 
Powiązane problemy