UIPickerView 导致崩溃 [英] UIPickerView causing crash

查看:43
本文介绍了UIPickerView 导致崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试在我的应用程序中选择 UIPickerView 时,它就会崩溃.

Whenever I try to select the UIPickerView in my App it crashes.

我已经实现了所有委托方法,但收到此错误:

I have implemented all the delegate methods but receive this error:

2013-01-15 13:57:56.176 tracker[16142:c07] *** Assertion failure in -[UITableViewRowData rectForRow:inSection:], /SourceCache/UIKit_Sim/UIKit-2372/UITableViewRowData.m:1630
2013-01-15 13:57:56.177 tracker[16142:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path (<NSIndexPath 0x918c700> 2 indexes [0, 0])'
*** First throw call stack:
(0x1c97012 0x10d4e7e 0x1c96e78 0xb6af35 0x2050c0 0xc892e 0x457fc6 0x457eba 0x7ba5d 0x7e03b 0x2e689a 0x2e59db 0x2e711f 0x2e9d6d 0x2e9cec 0x2e1a68 0x4efc2 0x4f4a3 0x2d3aa 0x1ecf8 0x1bf2df9 0x1bf2ad0 0x1c0cbf5 0x1c0c962 0x1c3dbb6 0x1c3cf44 0x1c3ce1b 0x1bf17e3 0x1bf1668 0x1c65c 0x29bd 0x28e5 0x1)
libc++abi.dylib: terminate called throwing an exception

以下是实现的委托方法:

Here are the implemented delegate methods:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {

    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component { 
    return [classes count];
}


- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [classes objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    NSLog(@"Selected Class: %@. Index of selected color: %i", [classes objectAtIndex:row], row);
}

'classes' 是一个 NSMutableArray.

'classes' is an NSMutableArray.

以下是视图的连接方式:

Here is how the view is wired up:

被控制的视图叫做 MemberViewController.

The view controlled is called MemberViewController.

我的接口声明包含以下内容:

My interface declaration contains the following:

@interface MemberViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>

我做错了什么?

谢谢.

推荐答案

我的应用遇到了类似的问题.IIRC,作为数据源的数组已被弱声明,并且正在被释放,因此在接收到 didSelectRow 时数组计数为 0(声明它为强数组解决了问题).正如 Rohan Kapur 所指出的,有一个与从该方法返回 0 相关的错误.

I had a similar issue with my app. IIRC, the array that served as the data source had been declared weakly and it was being released such that the array count was 0 when didSelectRow was received (declaring it as strong resolved the issue). As noted by Rohan Kapur, there is a bug related to returning 0 from that method.

我建议在 numberOfRowsInComponentdidSelectRow 的开头记录数组计数,以确保类"确实包含您期望的内容.

I would suggest logging the array count at the beginning of both numberOfRowsInComponent and didSelectRow to ensure that "classes" really contains what you expect.

这篇关于UIPickerView 导致崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆