我是Xcode的新手。我现在想在我的应用中实现一个包含2列的UIPickerView
。第一列是数字(例如1 - 5),第二列是单位(例如,星期,星期)。例如1天,2天,3周。
我尝试了以下代码:
- (void)viewDidLoad
{
repeatLabel.text = @"Never ever";
repeatNoArray= [[NSMutableArray alloc]init];
[repeatNoArray addObject:@"Never"];
for (int i = 1; i<=5; i++)
{
[repeatNoArray addObject:[NSString stringWithFormat:@"%i",i]];
}
timeUnitArray = [[NSMutableArray alloc]initWithObjects:@"ever",nil];
timeUnit1Array = [[NSMutableArray alloc]initWithObjects:@"day",@"week",@"month",@"year",nil];
timeUnit2Array = [[NSMutableArray alloc]initWithObjects:@"days",@"weeks",@"months",@"years",nil];
repeatPicker.delegate = self;
repeatPicker.dataSource = self;
[super viewDidLoad];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
if (pickerView == repeatPicker && component == 0)
return [repeatNoArray count];
else if (pickerView == repeatPicker && component == 1)
{
if ([repeatPicker selectedRowInComponent:0] == 0)
return timeUnitArray.count;
else if ([repeatPicker selectedRowInComponent:0] == 1)
return timeUnit1Array.count;
else
return timeUnit2Array.count;
}
return nil;
};
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (pickerView == repeatPicker && component==0)
return [repeatNoArray objectAtIndex:row];
if (pickerView == repeatPicker && component==1)
{
if ([repeatPicker selectedRowInComponent:0] == 0)
return [timeUnitArray objectAtIndex:row];
else if ([repeatPicker selectedRowInComponent:0] == 1)
return [timeUnit1Array objectAtIndex:row];
else
return [timeUnit2Array objectAtIndex:row];
}
return nil;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (pickerView == repeatPicker )
{
NSInteger selectedIndex = [repeatPicker selectedRowInComponent:0];
if (component == 0 && !(selectedIndex < 0))
{
[repeatPicker reloadComponent:1];
[repeatPicker selectRow:0 inComponent:1 animated:YES];
}
else
{
// do nth
}
[repeatPicker reloadComponent:1];
NSString *RepeatValue = [repeatNoArray objectAtIndex:[repeatPicker selectedRowInComponent:0]];
NSString *RepeatPeriod = [timeUnitArray objectAtIndex:[repeatPicker selectedRowInComponent:1]];
repeatLabel.text = [[@"Every " stringByAppendingString:RepeatValue]stringByAppendingString: RepeatPeriod];
}
}
它非常适合显示不同的第二列,但是当我尝试在第二列上选择一个新值时,它会给我一个错误:
由于未捕获的异常'NSRangeException'而终止应用程序,原因:' * - [__ NSArrayM objectAtIndex:]:索引1超出边界[0 .. 0]' * 第一次抛出调用堆栈: (0x31195f4b 0x3b5d66af 0x310cc533 0x102e71 0x33b7c329 0x33b7f119 0x33e4f223 0x33a3aa5b 0x33b08407 0x33b04021 0x33b03adb 0x33adbab3 0x33e4edbf 0x33b7e0bf 0x3391a6e5 0x33b7dfed 0x33b7e401 0x339fe5b9 0x33921f69 0x310da821 0x310da72f 0x33912f0d 0x339ff225 0x33909011 0x33908da3 0x3358fc6b 0x3358b47b 0x3358b30d 0x3358ad1f 0x3358ab2f 0x3358485d 0x311611cd 0x3115eb71 0x3115eeb3 0x310c9c27 0x310c9a0b 0x35df0283 0x3396d049 0x103871 0x3badeab7) libc ++ abi.dylib:以NSException类型的未捕获异常终止
任何人都知道如何修改代码?
答案 0 :(得分:0)
我自己找到答案。我尝试将所有不同的数组分配到一个数组(repeatArray)。选择器现在可以正常工作。以下是更新后的代码。
- (void)viewDidLoad
{
repeatLabel.text = @"Never ever";
repeatNoArray= [[NSMutableArray alloc]init];
[repeatNoArray addObject:@"Never"];
for (int i = 1; i<=1000; i++)
{
[repeatNoArray addObject:[NSString stringWithFormat:@"%i",i]];
}
timeUnitArray = [[NSMutableArray alloc]initWithObjects:@"ever",nil];
timeUnit1Array = [[NSMutableArray alloc]initWithObjects:@"day",@"week",@"month",@"year",nil];
timeUnit2Array = [[NSMutableArray alloc]initWithObjects:@"days",@"weeks",@"months",@"years",nil];
repeatPicker.delegate = self;
repeatPicker.dataSource = self;
[super viewDidLoad];
}
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
if (pickerView == repeatPicker)
return 2;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
if (pickerView == repeatPicker && component == 0)
return [repeatNoArray count];
else if (pickerView == repeatPicker && component == 1)
{
if ([repeatPicker selectedRowInComponent:0] == 0)
repeatArray = timeUnitArray;
else if ([repeatPicker selectedRowInComponent:0] == 1)
repeatArray = timeUnit1Array;
else
repeatArray = timeUnit2Array;
return [repeatArray count];
}
return 0;
};
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (pickerView == repeatPicker && component==0)
return [repeatNoArray objectAtIndex:row];
if (pickerView == repeatPicker && component==1)
{
if ([repeatPicker selectedRowInComponent:0] == 0)
repeatArray = timeUnitArray;
else if ([repeatPicker selectedRowInComponent:0] == 1)
repeatArray = timeUnit1Array;
else
repeatArray = timeUnit2Array;
return [repeatArray objectAtIndex:row];
}
return nil;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (pickerView == repeatPicker)
{
NSInteger selectedIndex = [repeatPicker selectedRowInComponent:0];
if (component == 0 && !(selectedIndex < 0))
{
[repeatPicker reloadComponent:1];
}
else
{
//do nth
}
if (component == 0 && row == 0)
{
[repeatPicker selectRow:0 inComponent:0 animated:YES];
repeatLabel.text = @"Never ever";
}
else if(component == 1 && row == 0)
{
if (component == 0 && row == 0)
repeatLabel.text = @"Never ever";
}
else
{
[repeatPicker reloadComponent:1];
NSString *RepeatValue = [repeatNoArray objectAtIndex:[repeatPicker selectedRowInComponent:0]];
NSString *RepeatPeriod = [repeatArray objectAtIndex:[repeatPicker selectedRowInComponent:1]];
repeatLabel.text = [[[@"Every " stringByAppendingString:RepeatValue] stringByAppendingString: @" " ]stringByAppendingString: RepeatPeriod];
}
}