0

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

dan1st
  • 12,568
  • 8
  • 34
  • 67
Adarkuccio
  • 973
  • 4
  • 12
  • 24

1 Answers1

0

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).

stosha
  • 2,108
  • 2
  • 27
  • 29
  • First of all thanks for your answer, I would like to ask you how can I connect the IBOutlets of cells (which have their own separate xib files and are different classes) into the UIViewController file (which have his own xib with the UITableView inside it), I've always connected IBOutlets into the corrispondent .h files (for example DataTableViewCell.xib into the DataTableViewCell.h) – Adarkuccio Jun 12 '13 at 14:08