syedfa
syedfa

Reputation: 2819

Getting runtime exception when trying to dynamically adjust size of UITableView in iOS

I am trying to increase the height of my UITableView whenever the user adds a new row to the table. The code allows the user to successfully add rows to the tableView no problem, however, I am getting a runtime exception when I try to adjust the height of the table with it.

Here is my code:

- (IBAction)addRow:(id)sender {

    ...

    CGFloat maxDynamicTableHeight = 250.0f;
    NSInteger numberOfSections = [self numberOfSectionsInTableView:self.myTable];
    CGFloat runningHeight = 0.0f;

    for (int section = 0; section < numberOfSections && runningHeight < maxDynamicTableHeight; section++) {
        NSInteger numberOfRows = [self tableView:self.myTable numberOfRowsInSection:section];
        for (int row = 0; row < numberOfRows && runningHeight < maxDynamicTableHeight; row++) {
            NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:section];
            NSLog(@"what is my path object? %@", path);
            runningHeight += [self tableView:self.myTable heightForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];
        }
    }

    CGRect frame = self.choiceTable.frame;
    frame.size.height = (runningHeight > maxDynamicTableHeight) ? maxDynamicTableHeight : runningHeight;
    self.myTable.frame = frame;
}

What I also am doing in my code is outputting the NSIndexPath object to see if it is valid, and this is what I get:

what is my path object? <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}

The runtime exception I get is:

[MyViewController tableView:heightForRowAtIndexPath:]: unrecognized selector sent to instance 0x7f9d306aadb0 2015-10-12 01:02:09.300 munch[27412:5036398] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyViewController tableView:heightForRowAtIndexPath:]: unrecognized selector sent to instance 0x7f9d306aadb0' * First throw call stack:

Can anyone see what it is I'm doing wrong?

Upvotes: 0

Views: 45

Answers (1)

Paulw11
Paulw11

Reputation: 115051

The exception is telling you that your table view datsource hasn't implemented the method tableView:heightForRowAtIndexPath:

If you implement this method then the exception will go away

Upvotes: 1

Related Questions