如果我同时旋转2个组件,偶尔依赖于选择哪个数组,则下面的代码会造成崩溃,这些值将超出范围
我需要一些条件检查来阻止崩溃,但不知道怎么做
任何人都可以建议合适的代码来处理条件以避免崩溃
感谢
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
// Only calls the following code if component "0" has changed.
if (component == 0)
{
// Sets the global integer "component0Row" to the currently selected row of component "0"
component0Row = row;
// Loads the new values for the selector into a new array in order to reload the data.
NSDictionary *newDict = [[NSDictionary alloc]initWithDictionary:[pickerData objectForKey:[Letters objectAtIndex:component0Row]]];
NSArray *sortArray = [[NSArray alloc]initWithArray:[newDict allKeys]];
NSMutableArray *newValues = [[NSMutableArray alloc]initWithArray:[sortArray sortedArrayUsingSelector:@selector(localizedStandardCompare:)]];
self.Numbers = newValues;
component1Row = 0;
[self.myPicker selectRow:0 inComponent:1 animated:NO];
[newDict release];
[sortArray release];
[newValues release];
}
if(component ==1)
{
component0Row = [pickerView selectedRowInComponent:0];
component1Row =row;
}
}
答案 0 :(得分:0)
问题可能出在titleForRow
方法或numberOfRows
方法中。你必须确保你没有超出所讨论数组的范围。