Expand / Collapse UITableViewCell with different c

2020-08-01 06:50发布

I did a UITableView filled with a plist data source, and I did custom cells with Interface Builder (so I'm using xib files)

here's the part of code where I'm creating cells:

- (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;
}

then when I use the:

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

method to get the selected cell, I call this method to expand the cell:

- (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];

}

Now the problem is that since I've not done this before, I'm stuck on the logic on how to change the cell I want to show when expanded, that's because the method:

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

is searching for the right path and inserting a cell with the same cell xib file as the one I've loaded with my data at the start

how can I set a different cell (with a different xib, and different data) to show instead of the same as before?

basically I have this table working but in the expanding cell I see a copy of the cell you touch

1条回答
走好不送
2楼-- · 2020-08-01 07:35

You can do it like this:

In .h:

@interface TSViewController : UIViewController
{
    NSMutableArray* subCellIndexPaths;
}

In DataTableViewSubCell.xib create one "Table View Cell" (other Views you have to remove). Change Class for "Custom Class" to DataTableViewSubCell. Replay it for second cell.

In .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];

}

And of course you must create subCellIndexPaths anywhere (in init or viewDodLoad methods).

查看更多
登录 后发表回答