2015-11-17 18 views
5

私のテーブルには何らかの異常が発生しました。私は2つ以上のセクションを持つテーブルを作成したい、最初のセクションで私は他のカスタムセルを使用したい。UITableViewのセクションごとに異なるカスタムセルを使用する

だから私は、最初のセクションでは、私の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; 
    } 
} 

でこれを作成し、私は、他人にdetailCellを使用し、私の行のheaderCellを使用したいです。このコードは動作しますが、セクション2の行ではheaderCell "under" detailCellをまだ使用しているように見えます。 headerCell.xibにラベルを追加しましたが、まだdetailCellに表示されています。これはimageを参照してください。

私はすべてのセクションに1つのセル識別子を使用しているため、このすべてを考えると思います。誰にでも解決策がありますか?どうもありがとうございます。

+0

はこれを試してみてください。 – Gandalf

答えて

9

カスタムセルの各タイプには、それぞれ固有の識別子が必要です。あなたのコードは、すべてのセルに同じセル識別子を使用しようとしています。それは動作しません。

また、cellForRowAtIndexPath:ではなく、viewDidLoadに2つのセルタイプを登録します。はい、あなたは、2つの異なるセル識別子を使用する必要が

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

ありがとう、rmaddy! –

関連する問題