一个关于 UIPickerView 的 bug
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个关于 UIPickerView 的 bug相关的知识,希望对你有一定的参考价值。
首先,我下描写叙述一下bug的发生情况:
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; }
以上是关于一个关于 UIPickerView 的 bug的主要内容,如果未能解决你的问题,请参考以下文章
如果 UIPickerView 未触及,则 UIPickerView 选择第 0 行
UIPickerView selectedRowInComponent:当我尝试在第二个组件中设置数据时,0总是先返回零
从另一个 ViewController 重新加载 UIPickerView 的数据
PhoneGap:iOS 7 中的 UIPickerView 不会像在 iOS 6 中那样自动调整字体大小。关于如何在 iOS 7 中实现的任何解决方案?