ios - 尝试在 iOS 中动态调整 UITableView 的大小时出现运行时异常

标签 ios objective-c 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:]: 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:

谁能看出我做错了什么?

最佳答案

异常告诉你你的 TableView 数据源没有实现方法 tableView:heightForRowAtIndexPath:

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

关于ios - 尝试在 iOS 中动态调整 UITableView 的大小时出现运行时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33089696/

相关文章:

iphone - 从 iphone 应用程序控制位置服务

ios - 如何在数组类型为 JSON 时实现自定义搜索栏

ios - 从 NSManagedObject 请求数组中获取平均值

ios - 在函数中调用 IBOutlet 属性时获得 Nil

ios - 改善 UIWebView 初始化时间

ios - 使用 Xcode Asset Catalogs 进行 UIImage 缓存

ios - Nib 必须恰好包含一个顶级对象,该对象必须是 UITableViewCell 实例

ios - 将单元格插入 TableView 后调用哪个方法

ios - 匹配正则表达式时获取单个元素

ios - 过滤除数字和 '+' 操作数之外的 NSString 中的所有字符