0

So I used this helpful tutorial a while back: http://locassa.com/animate-uitableview-cell-height-change/ and it was working perfectly in iOS6. However now, as I've been updating a few of my apps to iOS7 the code doesn't work... I have no idea why. The code checks out fine and it's logging which cell has been selected and which hasn't using an NSMutableDictionary at first I though it just wasn't ordering the selections in the Dictionary properly but that wasn't it. I've come to the possible conclusion that it could be something that changed when Apple updated their iOS. here is my code below:

First I'm logging whether the cell is selected 'selectedIndexes' is my NSMutableDictionary.

- (BOOL)cellIsSelected:(NSIndexPath *)indexPath {
// Return whether the cell at the specified index path is selected or not
NSNumber *selectedIndex = [selectedIndexes objectForKey:indexPath];
return selectedIndex == nil ? FALSE : [selectedIndex boolValue];
}

then in my didSelect method :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
BOOL isSelected = ![self cellIsSelected:indexPath];
    if ([self cellIsSelected:indexPath]) {
//hide objects that are in expanded area of cell
}else {
//load objects as cell is selected
}
// Store cell 'selected' state keyed on indexPath
    NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
    [selectedIndexes setObject:selectedIndex forKey:indexPath];
    [self.tableView beginUpdates];
    [self.tableView endUpdates];

So now I've setup what I want to happen when the cell is selected i've defined my desired height (kCellHeight):

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

if([self cellIsSelected:indexPath])
    return kCellHeight * 7;
 else
     return kCellHeight;

}

This worked with no probelsm before but for some reason now some of the cells will expand and others won't... which I really don't understand because the dictionary is logging the correct indexPath along with its values, selected or not selected. Hopefully someone who's had a similar issue might be able to point me in the right direction? Thanks.

Oliver
  • 23
  • 5
  • As a side note, it might be easier to store data about selections as a set. Sets are awesome! And if you use `NSIndexSet` you don't need to cast primitive integers to `NSNumber`. – ilya n. Sep 27 '13 at 09:36

1 Answers1

1

I'm not sure why this worked as-is on iOS 6, but to make sure system knows about your cell changing you're supposed to call self.tableView reloadRowsAtIndexPaths:...

ilya n.
  • 18,398
  • 15
  • 71
  • 89
  • Ahh ok - That could be it actually. I'll try that and see if it fixes the problem. – Oliver Sep 27 '13 at 09:49
  • I've tried using the `self.tableView reloadRowsAtIndexPaths:...` but it didn't seem to make much difference :/ . Thanks for the suggestion though! – Oliver Sep 27 '13 at 10:56
  • That is strange. Does `heightForRowAtIndexPath:` get called after cell selection if you add `reloadRowsAtIndexPaths` and remove `[self.tableView beginUpdates];`? – ilya n. Sep 27 '13 at 11:00
  • Yeh That works fine. I think it's the multiple selections that it has trouble with... for some reason it's not reading the contents of the Dictionary properly.. Ok so I've managed to get a functioning alternative which works fine for me because it's better only being able to expand one cell at a time! Can find the result I got from the answer at the bottom here : http://stackoverflow.com/questions/18853293/collapse-uitableviewcell-to-original-size-when-another-cell-is-clicked?rq=1 – Oliver Sep 27 '13 at 11:18