Trouble with rowHeight


#1

Hi everyone,

I have some trouble with the table view row height resizing, it just doesn’t work when the notification is posted as the user change the preferred dynamic type text size; here’s the code (same as the book obviously), but just in case:

On init:

NSNotificationCenter *defaultC = [NSNotificationCenter defaultCenter]; [defaultC addObserver:self selector:@selector(updateTableViewForDynamicTypeSize) name:UIContentSizeCategoryDidChangeNotification object:nil];

The method that gets the job done:

[code]- (void)updateTableViewForDynamicTypeSize
{
static NSDictionary *cellHeightDictionary;

if (!cellHeightDictionary) {
    cellHeightDictionary = @{UIContentSizeCategoryExtraSmall: @44,
                             UIContentSizeCategorySmall: @44,
                             UIContentSizeCategoryMedium: @44,
                             UIContentSizeCategoryLarge: @44,
                             UIContentSizeCategoryExtraLarge: @55,
                             UIContentSizeCategoryExtraExtraLarge: @65,
                             UIContentSizeCategoryExtraExtraExtraLarge: @75};
    
    NSString *userSize = [[UIApplication sharedApplication] preferredContentSizeCategory];
    NSNumber *cellHeight = cellHeightDictionary[userSize];
    [self.tableView setRowHeight:cellHeight.floatValue];
    [self.tableView reloadData];
}

}[/code]

And the notification been deallocated

- (void)dealloc { NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; [nc removeObserver:self]; }

Thanks in advance anyone that can help me. :slight_smile:


#2

This code:

NSString *userSize = [[UIApplication sharedApplication] preferredContentSizeCategory];
NSNumber *cellHeight = cellHeightDictionary[userSize];
[self.tableView setRowHeight:cellHeight.floatValue];
[self.tableView reloadData];

should be outside of the “if (!cellHeightDictionary) {” block of code. The code in that if block will only get called the first time that method is called, since cellHeightDictionary is only nil the first time the method is called (due to the static nature of the variable).