2013-02-07 6 views
14

3種類のプロトタイプセルを持つテーブルビューに関して簡単な質問がありました。最初の2回は1回だけ発生し、3回目は4回発生します。今私が混乱しているのは、どのセルのプロトタイプをどの行に使うかをcellforRowatindexpathで指定する方法です。だから、行0、プロトタイプ1、行1、プロトタイプ2、行3,4,5、および6用のプロトタイプ3を使いたい。これを行うにはどうすればよいの?各プロトタイプに識別子を与えて、dequeueReusableCellWithIdentifier:CellIdentifierを使用しますか? サンプルコードを提供できますか?複数のプロトタイプセルを持つTableView

編集:

まだ動作しません。これは私が今使っているコードです。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    switch(indexPath.row) 
{   
case 0: {static NSString *CellIdentifier = @"ACell"; 
        UITableViewCell *cell = [tableView 
              dequeueReusableCellWithIdentifier:@"ACell"]; 
    if(cell==nil) { 
    cell=[[UITableViewCell alloc] 
      initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"ACell"]; 

         } 
    return cell; 
    break; 
    } 
    } 
} 

Acellはのための私の識別子です(私はテストし、セルが最初の行で生成されたかされているかどうかを確認したいので、私は唯一のswitch文のための1つのケースを持っていますが、現在はテーブルビューは空白です)私が作成したセルプロトタイプ。 I

+0

。 –

+2

"各プロトタイプに識別子を与えてから、dequeueReusableCellWithIdentifier:CellIdentifier?を使用しますか?そうです。あなたはすでにあなた自身の質問に答えてきました。 – rdelmar

+0

しかしどのプロトタイプをどの行に適用するかを選択するにはどうすればよいですか? –

答えて

16

3つのプロトタイプを使用している場合は、3つの識別子を使用します。 1つの識別子だけが問題を引き起こすためです。そしてあなたは間違った結果を得るでしょう。このようにコード。

if(indexPath.row==0){ 
// Create first cell 
} 

if(indexPath.row==1){ 
// Create second cell 
} 

else{ 
// Create all others 
} 

スイッチケースは、ここでも最高のパフォーマンスを得るために使用できます。ここで

+0

セルの作成中に、単一の識別子で使用する前に、同じ方法を実行することができます。しかし、ここでは、dequeの間に3つの識別子を使用します。 –

3
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 

    if (cell.tag == 0) 
    { 
    return array1.count; 
    } 
    else(cell.tag == 1) 
    { 
    return array2.count; 
    }  
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *cellIdentifier; 

NSString *membershipType = [membershipTypeArray objectAtIndex:indexPath.row]; 

if ([membershipType isEqualToString:@"silver"]||[membershipType isEqualToString:@"gold"]) 
{ 
    cellIdentifier = @"cell"; 
} 
else if ([membershipType isEqualToString:@"platinum"]) 
{ 
    cellIdentifier = @"premiumCustomCell"; 
    cell.customCellImageView.image = [cellImageArray objectAtIndex:indexPath.row]; 
} 

cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

if (!cell) { 
    cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
} 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
cell.headingLabel.text = [titleArray objectAtIndex:indexPath.row]; 
} 
+0

どうやって処理するの?セクション内の行の数。プロトタイプセルが2つあり、配列数に基づいて表示される行がない場合は? –

+0

カスタムプロトタイプセルにタグを付け、各セルの配列数を確認して 'array.count'を返すだけです –

+0

@surajkthomas上記の編集を参照してください –

1

私のようなコードを書いた:私はあなたの質問をundertandません

#pragma mark == Tableview Datasource 

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
NSInteger nRows = 0; 
switch (section) { 
    case 0: 
     nRows = shipData.count; 
     break; 
    case 1: 
     nRows = dataArray1.count; 
     break; 
    default: 
     break; 
} 
return nRows; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
NSString *cellIdentifier = @"cellIdentifier1"; 
NSString *cellIdentifier1 = @"cellIdentifier2"; 
SingleShippingDetailsCell *cell; 
switch (indexPath.section) { 
    case 0: 
     cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
     //Load data in this prototype cell 
     break; 
    case 1: 
     cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1]; 
     //Load data in this prototype cell 
     break; 
    default: 
     break; 
} 
return cell; 
} 
関連する問題