一个关于 UIPickerView 的 bug

首先,我下描写叙述一下bug的发生情况:


在使用UIPickerView实现选择城市的时候。出现这样一个Bug
1、在iOS 6的系统上
2、Picker的数据上省份一栏选择了“香港”或者“澳门”这些没有二级数据的地区
3、然后滑动PickerView的二级选项。结果崩溃,并且不跳不论什么Delegate
4、这个问题在iOS 7或者更高系统的手机上是不存在的。


bug提示信息例如以下:

2014-07-28 19:46:00.711 BaiHe[1719:907] *** Assertion failure in -[UITableViewRowData rectForRow:inSection:], /SourceCache/UIKit/UIKit-2380.17/UITableViewRowData.m:1630

2014-07-28 19:46:00.915 BaiHe[1719:907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path (<NSIndexPath 0x1fb81370> 2 indexes [0, 0])'

*** First throw call stack:

(0x340ae2a3 0x3bd2e97f 0x340ae15d 0x34983ab7 0x35ee6ffb 0x35f04919 0x3606ec63 0x3623e8ef 0x35fd923b 0x35ff26ed 0x35fd3d89 0x35f9b3f5 0x36188a39 0x35ebf82f 0x35ebe293 0x35ec91e7 0x35ec8db3 0x35eb6801 0x35eb611b 0x37ba85a3 0x37ba81d3 0x34083173 0x34083117 0x34081f99 0x33ff4ebd 0x33ff4d49 0x37ba72eb 0x35f0a301 0x399cdf 0x26370)

libc++abi.dylib: terminate called throwing an exception


找了非常久。从网上网友处看到了一个解决方式,可是我试了一下。貌似缺少了些什么,所以我把解决方式的所有代码上传,希望能够帮助遇到相同问题的朋友。


解决方式:


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    //假设当前component中相应的数组为空。推断一些假设为空,返回1
    if ([[self.pickerArray objectAtIndex: component] count] <= 0)
    {
        return 1;
    }
    
    return [[self.pickerArray objectAtIndex: component] count];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = (UILabel *)view;
    
    if (!label)
    {
        label = [[[UILabel alloc] initWithFrame: CGRectMake(0, 0, 320, 20)] autorelease];
        label.backgroundColor = [UIColor clearColor];
        label.textAlignment = UITextAlignmentCenter;
        label.adjustsFontSizeToFitWidth = YES;
        label.numberOfLines = 2;
        label.minimumFontSize = 8.0f;
    }
    
    label.text = [[self.pickerArray objectAtIndex: component] count] ? [[[self.pickerArray objectAtIndex: component] objectAtIndex:row] objectForKey: @"hintValue"] : @"";
    
    //这里加上这段推断就可以
    if ([[self.pickerArray objectAtIndex: component] count] <= 0)
    {
        label.userInteractionEnabled = NO;
    }
    else
    {
        label.userInteractionEnabled = YES;
    }

    return label;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要自定义一个 UIPickerView,你需要遵循 UIPickerViewDataSource 和 UIPickerViewDelegate 协议,并实现其中的方法。 首先,你需要创建一个 UIPickerView 实例,并设置其数据源和代理为当前视图控制器。 ```swift class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate { let pickerView = UIPickerView() override func viewDidLoad() { super.viewDidLoad() pickerView.dataSource = self pickerView.delegate = self view.addSubview(pickerView) } } ``` 接着,你需要实现 UIPickerViewDataSource 协议中的两个必须方法:numberOfComponents(in:) 和 numberOfRows(inComponent:)。前者用于返回选择器中的列数,后者用于返回每列中的行数。 ```swift func numberOfComponents(in pickerView: UIPickerView) -> Int { return 2 } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return 10 } ``` 这里我们设置了两列,每列都有 10 行。 然后,你需要实现 UIPickerViewDelegate 协议中的方法来自定义选择器的样式和行为。比如,你可以自定义每一行的标题和宽度,以及选择器的动画效果。 ```swift func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return "Row \(row)" } func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat { return 100 } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { print("Selected row \(row) in component \(component)") } ``` 这里我们简单地返回了每一行的标题为 "Row \(row)",每列的宽度为 100,当选择器的行被选中时,会在控制台输出相应的信息。 最后,你需要将 UIPickerView 添加到你的视图中,并设置它的位置和大小。 ```swift pickerView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 200) ``` 这里我们将选择器的宽度设置为与视图宽度相同,高度为 200,然后将其添加到视图的顶部。 这样,你就可以自定义一个 UIPickerView 了!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值