我已经在我的故事板中连接了一个UIStackView,我正在动态添加按钮,如果我添加一个或两个按钮,但是当我想插入第三个按钮时应用程序崩溃时出现以下错误
017-12-27 10:41:14.315786+0800 NWMPos[39434:24150577] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'index out of bounds for arranged subview: index = 2 expected to be less than or equal to 1'
*** First throw call stack:
(0x187151d04 0x1863a0528 0x187151c4c 0x190ce44c4 0x104ec1fdc 0x106ae549c 0x106ae545c 0x106aea050 0x1870f9eb0 0x1870f7a8c 0x187017fb8 0x188eaff84 0x1905ec2e8 0x104ec4620 0x186b3a56c)
libc++abi.dylib: terminating with uncaught exception of type NSException
我没有在stackview中指定任何(我知道的)2个条目的限制,所以我在这里有点迷失为什么我可以添加两个按钮而不是第三个
以下是添加按钮的代码
dispatch_async(dispatch_get_main_queue(), ^{
_fcVariantImageView.image = nil;
if ([finalUrl hasPrefix:@"https"]) {
NSURL *url = [[NSURL alloc] initWithString:[finalUrl stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
NSData *data =[NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
_fcVariantImageView.contentMode = UIViewContentModeScaleAspectFit;
_fcVariantImageView.image = image;
} else {
_fcVariantImageView.image = [UIImage imageNamed:fcVariantRow[@"fcVariantImageUrl"]];
}
if ([finalSwatchUrl hasPrefix:@"https"]) {
NSURL *url = [[NSURL alloc] initWithString:[finalSwatchUrl stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
NSData *data =[NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
UIButton *imageButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
imageButton1.tag = 1;
imageButton1.frame = CGRectMake(0, 0, 4, 4);
[imageButton1 setImage:image forState:UIControlStateNormal];
[imageButton1 addTarget:self action:@selector(buttonPushed:) forControlEvents:UIControlEventTouchUpInside];
[_fcVariantColourStackView insertArrangedSubview:imageButton1 atIndex:0];
}
// If 2 variants we need add a second swatch
if(numberOfVariants == 2) {
NSDictionary *fcVariantRow2 = [_fullConceptVariants objectAtIndex:1];
//NSString *finalUrl2 = [NSString stringWithFormat:@"https:%@", fcVariantRow2[@"fcVariantImageUrl"]];
NSString *finalSwatchUrl2 = [NSString stringWithFormat:@"https:%@", fcVariantRow2[@"fcVariantSwatch"]];
if ([finalSwatchUrl2 hasPrefix:@"https"]) {
NSURL *url2 = [[NSURL alloc] initWithString:[finalSwatchUrl2 stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
NSData *data2 =[NSData dataWithContentsOfURL:url2];
UIImage *image2 = [UIImage imageWithData:data2];
UIButton *imageButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
imageButton2.tag = 2;
imageButton2.frame = CGRectMake(0, 0, 4, 4);
[imageButton2 setImage:image2 forState:UIControlStateNormal];
[imageButton2 addTarget:self action:@selector(buttonPushed:) forControlEvents:UIControlEventTouchUpInside];
[_fcVariantColourStackView insertArrangedSubview:imageButton2 atIndex:1];
}
}
// If 3 variants we need add a third swatch
if(numberOfVariants == 3) {
NSDictionary *fcVariantRow3 = [_fullConceptVariants objectAtIndex:2];
NSString *finalSwatchUrl3 = [NSString stringWithFormat:@"https:%@", fcVariantRow3[@"fcVariantSwatch"]];
if ([finalSwatchUrl3 hasPrefix:@"https"]) {
NSURL *url3 = [[NSURL alloc] initWithString:[finalSwatchUrl3 stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
NSData *data3 =[NSData dataWithContentsOfURL:url3];
UIImage *image3 = [UIImage imageWithData:data3];
UIButton *imageButton3 = [UIButton buttonWithType:UIButtonTypeCustom];
imageButton3.tag = 3;
imageButton3.frame = CGRectMake(0, 0, 4, 4);
[imageButton3 setImage:image3 forState:UIControlStateNormal];
[imageButton3 addTarget:self action:@selector(buttonPushed:) forControlEvents:UIControlEventTouchUpInside];
[_fcVariantColourStackView insertArrangedSubview:imageButton3 atIndex:2];
}
}
_fcVariantDescriptionLbl.text = fcVariantRow[@"fcVariantName"];
_fcVariantViewItemId.text = fcVariantRow[@"fcVariantItemId"];
_fcVariantViewPriceLbl.text = [NSString stringWithFormat:@"%@ %@", [NWTillHelper getCurrencySymbol], fcVariantRow[@"fcVariantPrice"]];
_fcSelectedColorLbl.text = fcVariantRow[@"fcVariantColourDescription"];
[_fcVariantSpinner stopAnimating];
});