2012-05-05 10 views
2

私はたくさんの検索をしていますが、複数のカスタム行に関連する便利なものは見つかりませんでした。x30ファイルから行をロードする必要があるアプリケーションの設定tableViewを作成する必要があります。複数のカスタム行UITableView?

ROW 1 = >> XIB 1
ROW 2 = >> XIB 2
ROW 3 = >> XIB 3
ROW 4 = >> XIB 4

マイ本コード:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell=nil; 
    //We use CellType1 xib for certain rows 
    if(indexPath.row==0){ 
     static NSString *CellIdentifier = @"ACell"; 
     cell =(ACell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if(cell==nil){ 
      NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"ACell" owner:self options:nil]; 
      cell = (ACell *)[nib objectAtIndex:0]; 
     } 
     //Custom cell with whatever 
     //[cell.customLabelA setText:@"myText"] 
    } 
    //We use CellType2 xib for other rows 
    else{ 
     static NSString *CellIdentifier = @"BCell"; 
     cell =(BCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if(cell==nil){ 
      NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"BCell" owner:self options:nil]; 
      cell = (BCell *)[nib objectAtIndex:0]; 
     } 
     //Custom cell with whatever 
     //[cell.customLabelB setText:@"myText"] 
    } 

    return cell; 
} 

答えて

7

まずあなたは、多くとしてあなたがXIBファイルを持っているように、いくつかのカスタムUITableViewCellのクラス(.Hおよび.M)を作成します。
CellType1.hが次にあなたがXIBファイルを作成

#import <UIKit/UIKit.h> 
@interface CellType1 : UITableViewCell 

@property(nonatomic,strong) IBOutlet UILabel *customLabel; 

@end 

ようになり、デフォルトのビュータイプを使用することができますが、その後、ちょうど、自動的に作成されたビューを削除UITableViewCellのことでそれを置き換える、および変更クラスをCellType1に設定します。 CellType2についても同じ処理を行います。

は、その後、あなたのtableViewControllerに、このようcellForRowを書く:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell=nil; 
//We use CellType1 xib for certain rows 
if(indexPath.row==<whatever you want>){ 
    static NSString *CellIdentifier = @"CellType1"; 
    cell =(CellType1*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell==nil){ 
     NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"CellType1" owner:self options:nil]; 
     cell = (CellType1 *)[nib objectAtIndex:0]; 
     } 
     //Custom cell with whatever 
     [cell.customLabel setText:@"myText"] 
} 
//We use CellType2 xib for other rows 
else{ 
    static NSString *CellIdentifier = @"CellType2"; 
    cell =(CellType2*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell==nil){ 
     NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"CellType2" owner:self options:nil]; 
     cell = (CellType2 *)[nib objectAtIndex:0]; 
     } 
     //Custom cell with whatever 
     [cell.customLabel setText:@"myText"] 
} 

return cell; 
} 
+0

"<何が欲しいもの>"とはどういう意味ですか? – Mateus

+0

複数のXibを使用したいとします.1行目xib1、2行目xib2などを推測しています。したがって、if/else文は行をチェックしますあなたが達成したいと思っているものに基づいて正しい行インデックス –

+0

で正しいxibを選んでください。私のXIBのメインビューは、UITableViewCellsを変更するか、単にビューのサイズを変更する必要がありますか? – Mateus

3

xibからカスタムセルを読み込むことに慣れていない場合は、documentation hereをチェックしてください。これを複数のカスタムxibに拡張するには、各テーブルセルを別々のxibに作成し、一意のセル識別子を与え、テーブルビューコントローラをファイルの所有者として設定し、各セルを定義したカスタムセルコンセントに接続しますこの出口としてtvCellを使用しています)。次に、-tableView:cellForRowAtIndexPath:メソッドでは、セルを提供している行を調べることによって、適切なxibをロードする(または正しい再利用可能なセルをデキューする)ことができます。たとえば:
ですから、たとえばCellType1とCellType2を持つことができます:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *MyIdentifier1 = @"MyIdentifier1"; 
    static NSString *MyIdentifier2 = @"MyIdentifier2"; 
    static NSString *MyIdentifier3 = @"MyIdentifier3"; 
    static NSString *MyIdentifier4 = @"MyIdentifier4"; 

    NSUInteger row = indexPath.row 

    UITableViewCell *cell = nil; 

    if (row == 0) { 
     cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifer1]; 
     if (nil == cell) { 
      [[NSBundle mainBundle] loadNibNamed:@"MyTableCell1" owner:self options:nil]; 
      cell = self.tvCell; 
      self.tvCell = nil; 
     } 
    } 
    else if (row == 1) { 
     cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifer2]; 
     if (nil == cell) { 
      [[NSBundle mainBundle] loadNibNamed:@"MyTableCell2" owner:self options:nil]; 
      cell = self.tvCell; 
      self.tvCell = nil; 
     } 
    } 
    else if (row == 2) { 
     cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifer3]; 
     if (nil == cell) { 
      [[NSBundle mainBundle] loadNibNamed:@"MyTableCell3" owner:self options:nil]; 
      cell = self.tvCell; 
      self.tvCell = nil; 
     } 
    } 
    else if (row == 4) { 
     cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifer4]; 
     if (nil == cell) { 
      [[NSBundle mainBundle] loadNibNamed:@"MyTableCell4" owner:self options:nil]; 
      cell = self.tvCell; 
      self.tvCell = nil; 
     } 
    } 
    // etc. 

    // Do any other custom set up for your cell 

    return cell; 

} 
+0

この方法は、私のために働いていません。私は 'UITableViewのdataSourceはtableViewからセルを返す必要があります:cellForRowAtIndexPath:'思考? ViewControllerを各セルにリンクさせ、カスタムIBOutletをセルごとに作成してセルに配線します。 –

関連する問題