0

plistデータソースで満たされたUITableViewを作成し、Interface Builderでカスタムセルを作成しました(したがって、xibファイルを使用しています)

セルを作成しているコードの部分は次のとおりです。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    DataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"DataTableViewCell" owner:nil options:nil];

        for (UIView *view in views) {
            if ([view isKindOfClass:[UITableViewCell class]]) {

                cell = (DataTableViewCell*)view;

            }
        }
    }

    return cell;
}

次に、次を使用する場合:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

メソッドを使用して選択したセルを取得し、このメソッドを呼び出してセルを展開します。

- (void)expandCellAtIndex:(int)index {

    NSMutableArray *path = [NSMutableArray new];
    NSArray *currentCells = [subCellItems objectAtIndex:index];
    int insertPos = index + 1;
    for (int i = 0; i < [currentCells count]; i++) {
        [path addObject:[NSIndexPath indexPathForRow:insertPos++ inSection:0]];
    }

    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

}

問題は、これまでにこれを行ったことがないため、展開したときに表示するセルを変更する方法についてのロジックに固執していることです。これは、メソッドが原因です。

[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];

正しいパスを検索し、最初にデータをロードしたものと同じセル xib ファイルを持つセルを挿入しています

以前と同じではなく、別のセル (別の xib と別のデータ) を表示するように設定するにはどうすればよいですか?

基本的にこのテーブルは機能していますが、拡大するセルには、タッチしたセルのコピーが表示されます

4

1 に答える 1

0

次のように実行できます。

.h:

@interface TSViewController : UIViewController
{
    NSMutableArray* subCellIndexPaths;
}

DataTableViewSubCell.xib で、「テーブル ビュー セル」を 1 つ作成します (他のビューは削除する必要があります)。「カスタム クラス」のクラスを DataTableViewSubCell に変更します。2番目のセルで再生します。

.m:

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

    DataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        if ([subCellIndexPaths containsObject: indexPath])
        {
            NSArray* nib = [[NSBundle mainBundle] loadNibNamed: @"DataTableViewSuperCell"
                                                         owner: nil
                                                       options: nil];

            cell = [nib objectAtIndex: 0];
        }
        else
        {
            NSArray* nib = [[NSBundle mainBundle] loadNibNamed: @"DataTableViewSubCell"
                                                         owner: nil
                                                       options: nil];

            cell = [nib objectAtIndex: 0];
        }
    }

    return cell;
}

- (void) expandCellAtIndex: (int)index
{
    NSMutableArray* indexPaths = [NSMutableArray new];
    NSArray* currentCells = [subCellItems objectAtIndex: index];

    int insertPos = index + 1;

    for (int i = 0; i < [currentCells count]; i++)
    {
        NSIndexPath* indexPath = [NSIndexPath indexPathForRow: insertPos++
                                                    inSection: 0];
        [indexPaths addObject: indexPath];
        [subCellIndexPaths addObject: indexPath];
    }

    [self.tableView insertRowsAtIndexPaths: indexPaths
                          withRowAnimation: UITableViewRowAnimationFade];

    [self.tableView scrollToRowAtIndexPath: [NSIndexPath indexPathForRow: index
                                                               inSection: 0]
                          atScrollPosition: UITableViewScrollPositionTop
                                  animated: YES];

}

そしてもちろん、subCellIndexPaths を任意の場所 (init または viewDodLoad メソッド内) に作成する必要があります。

于 2013-06-12T04:46:51.990 に答える