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
You can do it like this:
In .h:
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:
And of course you must create subCellIndexPaths anywhere (in init or viewDodLoad methods).