1

ストーリーボードを使用して iPad 6.0 でアプリケーションを開発しています。

まず、私の目標を説明させてください。2 つの UITableViewControllers を使用して、Master-Detail (SplitViewController のような) ビュー コントローラーを実現しようとしています。

最初の UITableView("Master") は、名前が示すように、このHeaderTableViewと呼びましょう...

...2 番目の UITableView("Detail")、これをEncodingTableViewと呼びましょう。これには、プログラムによって変更される CustomTableViewCell が含まれます (各セルに含まれるサブビューは、UITextField、UIButton、または UISwitch の場合があります)。

EncodingTableView.mを参照してください。

- (void)updateEncodingFields:(NSArray *)uiViewList
{
    // Add logic for determining the kind of UIView to display in self.tableView

    // Finally, notify that a change in data has been made (not working)
    [self.tableView reloadData];
}
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            NSString *encodingFieldsTableId = @"encodingFieldsTableId";

            CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:encodingFieldsTableId];

            if (cell == nil) {
                cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:encodingFieldsTableId];
            }
            // Change text in textView property of CustomTableViewCell
            cell.encodingFieldTitle.text = uiViewList.title;
            // added methods for determining what are to be added to [cell.contentView addSubView:]
            // data used here is from the array in updateEncodingFields:

        }

私のHeaderTableView.mには、 EncodingTableViewを更新するための didSelectRowAtIndexPath が含まれています

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
            if (![selectedIndexPath isEqual:indexPath]) {
                selectedIndexPath = indexPath;
                [self updateDataFieldTableViewForIndexPath:indexPath];
            }
        }

    - (void)updateDataFieldTableViewForIndexPath:(NSIndexPath *)indexPath {
        [self.encodingTableView updateEncodingFields:self.uiViewList];
    }


質問 - データはすべて問題ありませんが、EncodingTableViewがフィールドを「再描画」しないのはなぜですか? 細胞の再利用がこれと関係があるのではないかと疑っていますが、その理由はわかりません.

結果のスクリーンショット:


HeaderTableView での初期選択 初期選択

HeaderTableView での 2 番目の選択 二次選考

私が試したこと:

  • [UITableView setNeedsDisplay]、[UITableView reloadData]、[UITableView setNeedsLayout] などの提案が表示され続けましたが、どれも機能しませんでした。
  • tableViewCells の再利用を削除すると問題なく動作しますが、これにより CustomTableView.encodingFieldTitle の一部が消えます。言うまでもなく、再利用セルを削除すると、パフォーマンスの問題が発生する可能性があります。

制限: SplitViewController を使用することをお勧めしますが、これはアプリの一部にすぎません (したがって、RootViewController ではありません)。

最後に、長い記事を読んでくれてありがとう。;)

4

3 に答える 3

5

内にサブビューを追加している可能性が高いようですtableView:cellForRowAtIndexPath:

問題は、セルの再利用を使用する場合、常に内部の白紙の状態から開始するとは限らず、tableView:cellForRowAtIndexPath:代わりに、すでに一度構成されたセルを戻すことができる可能性があることです。これが表示されているものです。以前にラベルが追加されたセルが返され、その上にさらにいくつかのラベルが追加されます。

これに対処する方法はいくつかあります。

  1. (私の好みのオプション)UITableViewCellこれらの追加のサブビューをプロパティとして使用して、のサブビューを作成します。

  2. セルのセットアップが1回だけ行われるようにするこれを行うのに最適な場所は、チェック
    内など、セルがまだ存在しないときに実際にセルを作成する場合です。if (cell)

    if (cell == nil) {
      cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:encodingFieldsTableId];
    
      // add subview's here and give them some way to be referenced later
      // one way of doing it is with the tag property (YUK)
      UILabel *subView = [[UILabel alloc] initWithframe:someFrame];
      subView.tag = 1;
      [cell.contentView addSubview:subView];
    }
    
    UILabel *label = (id)[cell.contentView viewWithTag:1];
    label.text = @"some value";
    
于 2013-03-11T11:46:22.440 に答える
2

コードで確認できる問題の 1 つは、使用されているセル識別子が tableView cellForRowAtIndxPath 関数で異なることです。

デキュー中に、この識別子を使用しています - > "encodingFieldsTableId"

&

この識別子を使用しているセルの作成中に->「dataFieldUiGroupTableId」。

理想的には、これら 2 つの識別子は同じでなければなりません!!!

于 2013-03-11T11:02:52.637 に答える
1

追加してみて、

cell.encodingFieldTitle.text = nil;

if(cell == nil)

そのため、メソッドが呼び出されるたびに、cellForRowAtIndexPath再利用しようとしているセルに既に存在する文字列が削除され、新しいテキストuiViewList.titleが表示されます。

于 2013-09-03T12:18:58.080 に答える