2011-02-06 14 views

Odpowiedz

10

Osobiście uważam, że oba sugerowane tutoriale mają dużą wadę, jeśli chodzi o reuseIdentifier. Jeśli zapomnisz go przypisać w konstruktorze interfejsów lub źle przeliterować, ładujesz stalówkę za każdym razem, gdy zostanie wywołana cellForRowAtIndexPath.

Jeff LaMarche pisze o tym i jak to naprawić w tym blog post. Oprócz reuseIdentifier używa tego samego podejścia, co w dokumentacji Apple na Loading Custom Table-View Cells From Nib Files.

Po przeczytaniu wszystkich tych artykułów wymyśliłem następujący kod:

Edit: Jeśli są kierowane iOS 5.0 i wyższe będziesz chciał trzymać się Duane Fields' answer

@interface CustomCellWithXib : UITableViewCell 

+ (NSString *)reuseIdentifier; 
- (id)initWithOwner:(id)owner; 

@end 

@implementation CustomCellWithXib 

+ (UINib*)nib 
{ 
    // singleton implementation to get a UINib object 
    static dispatch_once_t pred = 0; 
    __strong static UINib* _sharedNibObject = nil; 
    dispatch_once(&pred, ^{ 
     _sharedNibObject = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil]; 
    }); 
    return _sharedNibObject; 
} 

- (NSString *)reuseIdentifier 
{ 
    return [[self class] reuseIdentifier]; 
} 

+ (NSString *)reuseIdentifier 
{ 
    // return any identifier you like, in this case the class name 
    return NSStringFromClass([self class]); 
} 

- (id)initWithOwner:(id)owner 
{ 
    return [[[[self class] nib] instantiateWithOwner:owner options:nil] objectAtIndex:0]; 
} 

@end 

UINib (dostępny w iOS 4.0 i późniejszych) jest używany tutaj jako singleton, ponieważ chociaż używa się reuseIdentifier, komórka nadal jest ponownie inicjowana około 10 razy. Teraz cellForRowAtIndexPath wygląda następująco:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomCellWithXib *cell = [tableView dequeueReusableCellWithIdentifier:[CustomCellWithXib reuseIdentifier]]; 
    if (cell == nil) { 
     cell = [[CustomCellWithXib alloc] initWithOwner:self]; 
    } 

    // do additional cell configuration 

    return cell; 
} 
+0

Moim zdaniem jest to najlepszy sposób. Dziękuję za to. Gdyby to był mój temat, oznaczałbym to jako odpowiedź. –

15

W iOS5 będziemy chcieli użyć nowego:

registerNib:forCellReuseIdentifier:

które w zasadzie robi to samo ...

+0

Fajnie, nie zauważyłem tego. Dzięki! – christoph

+0

Właśnie tego starałem się zapamiętać! Miły. – Ash

+1

Dokładniej, dodaj to do swojej viewDidLoad: [self.tableView registerNib: [nazwaNibWithNibName: @ "CustomCell" bundle: nil] forCellReuseIdentifier: @ "CustomCell"]; –

0

Można tworzyć Klasa CustomCell z XIB odziedziczonym z UITableViewCell. Dodamy kategorię w pliku mb viewview klasy w następujący sposób. Myślę, że jest to najłatwiejsza metoda, którą można zastosować do tworzenia niestandardowych komórek.

 

    @interface UITableViewCell(NIB) 
    @property(nonatomic,readwrite,copy) NSString *reuseIdentifier; 
    @end 
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
     return 30; 
    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    static NSString *[email protected]"cell"; 
     CustomCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier]; 
     if(cell==nil) 
     { 
      NSLog(@"New Cell"); 
      NSArray *nib=[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
      cell=[nib objectAtIndex:0]; 
      cell.reuseIdentifier=identifier; 

     }else{ 
      NSLog(@"Reuse Cell"); 
     } 
     cell.lbltitle.text=[NSString stringWithFormat:@"Level %d",indexPath.row]; 
     id num=[_arrslidervalues objectAtIndex:indexPath.row]; 
     cell.slider.value=[num floatValue]; 
     return cell; 
    } 
    @end 

1

`Można tworzyć niestandardowe komórki w widoku tabeli przy pomocy pliku .xib. Najpierw skonfiguruj widok tabeli w kontrolerze widoku, utwórz nowy plik Xib wraz z jego klasą i użyj go w widoku tabeli.

- (IBAction)moveToSubCategory:(id)sender; 
@property (strong, nonatomic) IBOutlet UILabel *foodCategoryLabel; 
@property (strong, nonatomic) IBOutlet UIImageView *cellBg; 



-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [foodCatArray count]; 
} 



-(UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *simpleTableIdentifier = @"ExampleCell"; 
     ExampleCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
     if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ExampleCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 
    [cell setTag:indexPath.row]; 
    cell.cellBg.image=[UIImage imageNamed:[photoArray objectAtIndex:indexPath.row]]; 
    cell.foodCategoryLabel.text=[foodCatArray objectAtIndex:indexPath.row]; 
    return cell; 

} 
Powiązane problemy