2

cellForRowAtIndexPath で次のコードを使用して、グループ化されたカスタム テーブルビュー スタイルを設定しました。各セルには新しい背景ビューが与えられ、セクション内の位置に応じて角が丸められます。私はアプリ全体でこのスタイルを使用し、さまざまなビューでアニメーションを使用して行を追加および削除しています。([tableview insertRowsAtIndexPaths:] などを使用)。

セクションの最後の行が挿入または削除された場合、セルの背景ビューの隅を再読み込みできる必要があります。[tableview reloadData]またはreloadCellsAtIndexPathsを呼び出さずにこれを行うにはどうすればよいですか? これらの関数はどちらも、セルの挿入と削除のアニメーションを台無しにします。適切なタイミングで呼び出されるこの背景コーナーの定義を配置できる場所はありますか? デフォルトのグループ化されたテーブルビューはどのようにこれを行いますか?

これが不明な場合は申し訳ありません。説明を求めてください。編集します。ありがとう!:)

UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:[self styleForCellAtIndexPath:indexPath] 
                                       reuseIdentifier:cellIdentifier] autorelease];

        // Theme the cell
            TableCellBackgroundView *backgroundView = [[TableCellBackgroundView alloc] initWithFrame:cell.frame];
            TableCellBackgroundView *selectedBackgroundView = [[TableCellBackgroundView alloc] initWithFrame:cell.frame];
            selectedBackgroundView.selectedStyle = YES;
            // top row, without header
            BOOL header = [self tableView:aTableView heightForHeaderInSection:indexPath.section]!=0;
            if (indexPath.row == 0 && (!header || tableTheme==kTableThemeSimple)) {
                backgroundView.topRadius = 6;
                selectedBackgroundView.topRadius = 6;
            }
            // bottom row
            if (indexPath.row == [self tableView:aTableView numberOfRowsInSection:indexPath.section]-1) {
                backgroundView.bottomRadius = 6;
                selectedBackgroundView.bottomRadius = 6;
            }
            cell.backgroundView = backgroundView;
            cell.selectedBackgroundView = selectedBackgroundView;
            cell.contentView.backgroundColor = [UIColor clearColor];
            cell.backgroundColor = [UIColor clearColor];
        }
    }
4

3 に答える 3

0

カスタム アニメーションに対して呼び出すメソッドがある場合、アニメーションが完了したとき、または新しいセルが表示される前に、UITableViewCell のサブクラスを使用して、- (void) fixCornersWithIndexPath: のような名前のメソッドを呼び出します。 (NSIndexPath *) インデックスパス。

- (void) customAnimationForCellAtIndexPath: (NSIndexPath *) path {
    //your animation things
    CustomCell *cell = [self.tableView cellForRowAtIndexPath: indexPath];
    [cell fixCornersWithIndexPath: path andRowCount: [self tableView:aTableView numberOfRowsInSection:indexPath.section]];
}

次に、コーナーを修正すると、次のようになります。

- (void) fixCornersWithIndexPath: (NSIndexPath *) indexPath andRowCount: (NSInteger *) rowCount {
      if (indexPath.row == 0 && (!header || tableTheme==kTableThemeSimple)) {
            self.backgroundView.topRadius = 6;
            self.selectedBackgroundView.topRadius = 6;
        }
        // bottom row
        if (indexPath.row == rowCount-1) {
            self.backgroundView.bottomRadius = 6;
            self.selectedBackgroundView.bottomRadius = 6;
        }
}

お役に立てれば!

于 2012-04-19T00:38:10.287 に答える
0

私のために働いた解決策はこれです:

  1. tableView:willDisplayCell:forRowAtIndexPath で背景を設定します。
  2. willDisplayCell を手動で呼び出す reloadBackgroundForRowAtIndexPath という便利なメソッドを作成します。
  3. 行を追加するときは、テーブル アニメーションを終了できるように、遅延後に便利なメソッドを呼び出します

    - (void)reloadBackgroundForRowAtIndexPath:(NSIndexPath*)indexPathToReload {
        [self tableView:self.tableView willDisplayCell:[self.tableView cellForRowAtIndexPath:indexPathToReload] forRowAtIndexPath:indexPathToReload];
    }
    
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        // Logic to determine the background image
    }
    
    // elsewhere, update the table, then
    NSIndexPath *indexPathToFix = [NSIndexPath indexPathForRow:count-1 inSection:0];
    // If the new row is coming out, we want to update the background right away.  If the new row is going away, we want to wait to update until the animation is done.
    // I don't like hard-coding the animation length here, but I don't think I have a choice.
    [self performSelector:@selector(reloadBackgroundForRowAtIndexPath:)  withObject:indexPathToFix afterDelay:(self.tableView.isEditing ? 0.0 : 0.2)];
    
于 2012-07-31T16:25:10.377 に答える
0

これがあなたが探しているものだと思います。モデルを変更したら、次を呼び出します。

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

これにより、cellAtIndexPath メソッドが呼び出されます。古い行の単一のインデックス パスを含む配列を送信します (丸みを帯びた角の問題については、行が削除された後の最後の行か、新しく追加された最後の行の前の行であると思います)。

開始/終了更新の間に、他の更新メソッドと一緒に呼び出すことができます。

于 2012-04-19T00:52:51.137 に答える