【发布时间】:2017-12-13 03:04:52
【问题描述】:
每当用户向表中添加新行时,我都会尝试增加 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:]: 无法识别的选择器发送到实例 0x7f9d306aadb0 2015-10-12 01:02:09.300 munch[27412:5036398] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[MyViewController tableView:heightForRowAtIndexPath:]:无法识别的选择器发送到实例 0x7f9d306aadb0” * 首先抛出调用栈:
谁能看出我做错了什么?
【问题讨论】:
-
我不确定你在哪里调用
tableView:heightForRowAtIndexPath:,因为它似乎不在显示的代码中,但它省略了inSection:参数,所以你得到一个无法识别的选择器异常 -
如果你向右滚动,它就在那里 :-)
-
不,它在那个代码中。异常告诉我你正试图在其他地方调用它没有 inSection - 我可以从异常消息中的方法签名中看出
-
我明白你在说什么。我很抱歉。我相信它指的是这一行: [self tableView:self.myTable heightForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];我为 NSIndexPath 对象提供了 section 参数,但没有为 heightForRowAtIndexPath 方法提供。我查了一下,heightForRowAtIndexPath 不需要section 参数。
-
哦,你是对的。对不起。但仍然有异常告诉您您的视图控制器没有实现与正在进行的调用匹配的方法
标签: ios objective-c uitableview