2015-11-17 13 views
5

Znalazłem dziwne rzeczy na moim stole. Chcę utworzyć tabelę z dwiema lub więcej sekcjami, a na pierwszej sekcji chcę użyć innej niestandardowej komórki z innymi.Użyj innej niestandardowej komórki dla każdej sekcji w UITableView

Stworzyłem więc to na moim tableView:cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"cell"; 
    if (indexPath.section == 0) { 
     // cell for section one 
     HeaderCell *headerCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
     if(!headerCell) { 
      [tableView registerNib:[UINib nibWithNibName:@"HeaderCell" bundle:nil] forCellReuseIdentifier:cellIdentifier]; 
      headerCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
     } 
     headerCell.labelName.text = @"First Section"; 
     return headerCell; 
    } 
    else { 
     // Cell for another section 
     DetailCell *detailCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
     if (!detailSection) { 
      [tableView registerNib:[UINib nibWithNibName:@"DetailCell" bundle:nil] forCellReuseIdentifier:cellIdentifier]; 
      detailCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
     } 
     detailCell.textLabel.text = @"Another Section Row"; 
     return detailCell; 
    } 
} 

Na pierwszym odcinku, chcę użyć headerCell dla mojego rzędu, a następnie użyć detailCell na innych. Ten kod działa, ale w wierszu sekcji drugiej nadal wygląda tak, jak przy użyciu headerCell "pod" detailCell. Dodałem etykietę do headerCell.xib i nadal jest wyświetlana na detailCell. Zobacz ten image.

Myślę, że wszystko to, ponieważ używam jednego identyfikatora komórki dla wszystkich sekcji. Ktoś ma rozwiązanie? Dziękuję bardzo.

+0

tak, trzeba użyć 2 różne identyfikatory komórek. – Gandalf

Odpowiedz

9

Każdy typ komórki niestandardowej powinien mieć swój unikalny identyfikator. Twój kod próbuje użyć tego samego identyfikatora komórki dla wszystkich komórek. To nie zadziała.

Zarejestruj również dwa typy komórek w viewDidLoad, a nie cellForRowAtIndexPath:.

Spróbuj tego:

static NSString *cellIdentifier0 = @"cell0"; 
static NSString *cellIdentifier1 = @"cell1"; 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == 0) { 
     // cell for section one 
     HeaderCell *headerCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier0 forIndexPath:indexPath]; 

     headerCell.labelName.text = @"First Section"; 

     return headerCell; 
    } else { 
     // Cell for another section 
     DetailCell *detailCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1 forIndexPath:indexPath]; 

     detailCell.textLabel.text = @"Another Section Row"; 

     return detailCell; 
    } 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // the rest of your code 

    [self.tableView registerNib:[UINib nibWithNibName:@"HeaderCell" bundle:nil] forCellReuseIdentifier:cellIdentifier0]; 
    [self.tableView registerNib:[UINib nibWithNibName:@"DetailCell" bundle:nil] forCellReuseIdentifier:cellIdentifier1]; 
} 
+0

Dziękuję bardzo, rmaddy! –

Powiązane problemy