15

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

編集:

まだ動作していません。これは私が現時点で持っているコードです。(最初の行でセルが生成されているかどうかをテストして確認したいだけなので、switchステートメントのケースは1つしかありませんが、現在テーブルビューは空白です)

- (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 は、私が作成したセル プロトタイプの識別子です。私

4

3 に答える 3

16

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

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

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

else{
 // Create all others
}

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

于 2013-02-07T19:00:57.663 に答える
1

ここで私は次のようなコードを書きました:

#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;
 }
于 2016-11-08T12:17:04.073 に答える