0

I have a UITableViewController that displays properties of an NSManagedObject in a grouped tableView. The values of those properties can be changed from custom UITableViewCell subclasses that have UISteppers and UITextFields. The table normally has 9 sections, but when some of the NSManagedObject's properties are nil, I hide those sections by setting numberOfRowsInSection to 0, as well as heightForHeaderInSection and heightForFooterInSection to 1. That works fine; the sections are hidden.

The custom cells call a delegate method that changes the NSManagedObject's property values, and the UITableViewController is set up to KVO observe the properties of the NSManagedObject, and reload its individual rows when the corresponding properties change. Here's the beginning of the method:

#define POSITION 3
#define TARGETED_BY 5

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"positionX"]) {
        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:POSITION]]
                              withRowAnimation:UITableViewRowAnimationNone];

    } else if ([keyPath isEqualToString:@"positionY"]) {
        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:POSITION]]
                              withRowAnimation:UITableViewRowAnimationNone];

    } else if ([keyPath isEqualToString:@"isTargetedBy"]) {
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:TARGETED_BY]
                      withRowAnimation:UITableViewRowAnimationNone];   
    }
}

It's not the complete method with all the properties, but you get the idea.

THE PROBLEM: when I've hidden the section above PROPERTIES, and I change the value of positionX, the cell at row 0 (the correct positionX cell) reloads not with UITableViewRowAnimationNone, but with what appears to be UITableViewRowAnimationFade. It moves, and I don't want it to. My subclass is directly from UITableViewController, and there's no other method in the class that reloads any rows or sections with anything other than -RowAnimationNone. And when I try to reload the whole section with no animation, the whole section animates. None of the other rows in the section behave like this; they reload correctly, with no animation. And row 0 reloads with no animation when the section above it is not hidden.

I can work around this by calling [self.tableview reloadData], but I'd rather not do it that way, for the sake of time and hackiness. Anyone have any idea how to prevent this behavior?

  • The oddest part (but perhaps a clue) is that this problem does not appear to exist on iPhone. Only on iPad. – Clayton Combe Mar 25 '13 at 12:41
  • Update: I managed to force it to not animate by calling [UIView setAnimationsEnabled:NO] at the beginning of the observeValueForKeyPath: method, then setting it back to YES at the end. I'm still not sure why this happens on this tableView only (there are other similar objects), but I'm willing to move past it for now, and hopefully this post will help someone else with a workaround. – Clayton Combe Mar 25 '13 at 15:09

1 Answers1

0

This can get complicated when many sections are involved. I think you're trying to replicate what the free Sensible TableView framework already does. The framework will automatically generate all your sections based on your managed object's data, and can determine which sections to make visible based on your own rules. Worth checking out as it could save you tons of time trying to reinvent all this.

Matt
  • 2,391
  • 2
  • 17
  • 18
  • Thanks Matt. What's frustrating is that it's only a very specific case that causes this behavior. The rest of the time, it works exactly like I want it to. I can't find any call to animate anything anywhere in the class. At this point, I don't think starting over with a framework will save me time, though, since I've already got it working 99% perfectly with my own subclass. – Clayton Combe Mar 26 '13 at 03:12