1

TableViewiOS でa をサポートするテキスト配列があります。: メソッドではcellForRowAtIndexPath、バッキング配列からのテキストが取り込まれた UITableViewCell* を返します。indexPath は、バッキング配列へのインデックスとして使用されます。

の最後のセルに「完了」ボタンを追加したいと思いますTableView。私の StoryBoard では、2 番目の (プロトタイプ)TableView Cellを作成し、「ButtonCell」という識別子を付けました。numberOfRowsInSection: がバッキング配列のカウントを返すことができ、すべてが機能するように、バッキング配列の最後に追加の要素も追加しました。

最後の配列要素のテキストを @"donebutton" のようなものに設定すると、cellForRowAtIndexPath: でそれを確認できると思いました。それが真であれば、配列の最後にいることがわかり、通常の「Cell」ではなく「ButtonCell」セルを返すことになります。問題は、それがうまく機能していないということです。これを達成するための最良の方法は何ですか? コードスニップは以下です。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"Cell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

   static NSString *ButtonCellIdentifier = @"ButtonCell";
   UITableViewCell *bcell = [tableView dequeueReusableCellWithIdentifier:ButtonCellIdentifier forIndexPath:indexPath];

   NSString *rowtext = [_mArCellData objectAtIndex:indexPath.row];

   // return button cell if last item in list
   if ([rowtext isEqualToString:[NSString stringWithFormat:@"%d", SUBMIT_BUTTON]])
   {
      NSLog(@"hit last row, so using button row");
      return bcell;
   }

   cell.textLabel.text = rowtext;
   return cell;
}

これは私が得ているものです

4

2 に答える 2

3
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    static NSString *ButtonCellIdentifier = @"ButtonCell";

    UITableViewCell *cell;

    if (indexPath.row != ([_mArCellData count] - 1) { // if not the last row

        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        // configure cell...

    } else { // last row

        cell = [tableView dequeueReusableCellWithIdentifier:ButtonCell];
        // configure button cell...
    }

    return cell;
}
于 2013-10-01T20:04:36.150 に答える
1

if ステートメントを次のように変更します。

if ([tableView numberOfRowsInSection:0] == indexPath.row + 1) {
   NSLog(@"hit last row, so using button row");
   bcell.textLabel.text = rowtext;
   return bcell;
}

これはソリューションよりも少し抽象化されており、セルのプロパティが特定のものに設定されていることに依存していません。@bilobatum が dequeueReusableCellWithIdentifier: if ステートメントに呼び出す方法が気に入っています。これにより、メモリも節約できるはずです。

編集:また、セル テキストを設定していることに気付きましたが、bcell テキストは設定していません。

于 2013-10-01T20:11:09.990 に答える