当尝试在iOS中动态调整UITableView的大小时获得运行时异常



我试图增加我的UITableView的高度每当用户添加一个新的行到表。代码允许用户成功地添加行到tableView没有问题,但是,我得到一个运行时异常,当我试图调整表的高度与它。

下面是我的代码:
- (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;
}

我在代码中所做的是输出NSIndexPath对象,看看它是否有效,这就是我得到的:

what is my path object? <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}
我得到的运行时异常是:

[MyViewController tableView:heightForRowAtIndexPath:]:无法识别的选择器发送到实例0x7f9d306aadb02015-10-12 01:02:09.300 munch[27412:5036398] *由于未捕获异常'NSInvalidArgumentException'而终止应用程序,原因:'-[MyViewController tableView:heightForRowAtIndexPath:]: unrecognized selector sent to instance 0x7f9d306aadb0'*第一个抛出调用栈:

有人能看出我做错了什么吗?

这个异常告诉你,你的表视图数据源没有实现方法tableView:heightForRowAtIndexPath:

如果你实现了这个方法,那么异常就会消失

最新更新