Reputation: 267
I would like the cell that is in section 1 of my tableView to resize automatically, upon perusing stack overflow I found a fairly well accepted answer and attempted to implement it. However using the following code causes the system to crash without an error.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath section] == 0) {
return 80;
}
else if([indexPath section] == 1){
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// NSLog(@"%@", cell.textLabel.text);
// NSString *cellText = cell.textLabel.text;
//
// UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
// CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
// CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
//
// return labelSize.height + 20;
NSLog(@"Hello");
return 120;
}
return 44;
}
However if I remove the creation of cell, like so:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath section] == 0) {
return 80;
}
else if([indexPath section] == 1){
// NSLog(@"%@", cell.textLabel.text);
// NSString *cellText = cell.textLabel.text;
//
// UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
// CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
// CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
//
// return labelSize.height + 20;
NSLog(@"Hello");
return 120;
}
return 44;
}
The error stops and the app runs fine. NOte the inclusion of an NSLog, when the cell creation is included this causes a stack overflow I think, as the console is loaded with repeating "Hello". This stops immediately when the cell creation is removed, and Hello is only said once, as expected. Don't mind the rest of the code, I'm mostly concerned with why the addition of this one line of code causes the reaction that it does.
Upvotes: 1
Views: 262
Reputation: 43330
You have what's called an infinite recursion.
In layman's terms, it means that UITableview's delegate method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
is called when [tableView cellForRowAtIndexPath:indexPath];
is called. Because you've called it inside one of the methods that it eventually calls, you create a nice infinite loop, thus printing out 7 million NSLogs and crashing your app (after a while of course).
Upvotes: 2