我在数据库中有产品表。产品表我有id& product_image列。每个图像都有id。我从数据库中获取这些id并被带到cat_imageidArray,然后获取图像&被带到productimg_array。将productimg_array图像值分配给imgView1(UIButton)。我正在使用[productimg_array objectAtIndex:i]。这里的索引是0到7或者等等。但cat_imageidArray的值为183,184,190等。当我点击imgView1按钮时,我收到这些错误[__NSArrayM objectAtIndex:]:索引197超出界限[0 .. 8]。我明白问题是数组值不匹配。如何将cat_imageidArray值分配给productimg_array索引。也许它会起作用。
const char *sql = "SELECT id,cat_id,product_image,order_by,description FROM product where cat_id = ?";
NSLog(@"sql is %s",sql);
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
// We "step" through the results - once for each row.
if (sqlite3_bind_int(statement, 1, categoryId) != SQLITE_OK)
NSLog(@"%s: bind failed: %s", __FUNCTION__, sqlite3_errmsg(database));
while (sqlite3_step(statement) == SQLITE_ROW) {
cat_iamgeId = sqlite3_column_int(statement, 0);
NSLog(@"cat_iamgeId is %ld",(long)cat_iamgeId);
[cat_imageidArray addObject:@(cat_iamgeId)];
product_image = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 2)];
NSLog(@"product_image is %@",product_image);
[productimg_array addObject:product_image];
}
}
for (int i = 0; i<[productimg_array count]; i++ ) {
NSLog(@"productimg_array_index %@", productimg_array[i]);
imgView1=[[UIButton alloc]initWithFrame:CGRectMake(20+(i*74), 0, 72, 72)];
Width = Width + 20+(i*74);
for (int i=0; i<[cat_imageidArray count]; i++){
NSLog(@"index %@", cat_imageidArray[i]);
tag=[[cat_imageidArray objectAtIndex:i]intValue];
imgView1.tag=tag;
NSLog(@"tag is %d",tag);
}
[imgView1 addTarget:self action:@selector(dbsofaClicked:) forControlEvents:UIControlEventTouchUpInside];
[imgView1 setImageWithURL:[NSURL URLWithString:[productimg_array objectAtIndex:i]]forState:UIControlStateNormal];
[scrollview addSubview:imgView1];
}
imgView1 button click:
-(void)dbsofaClicked:(id)sender{
NSLog(@"button %d is clicked.", [sender tag]);
mmageView=[[UIImageView alloc]initWithFrame:CGRectMake(50,50,150,150)];
[mmageView setUserInteractionEnabled:YES];
[mmageView setImageWithURL:[NSURL URLWithString:[productimg_array objectAtIndex:[sender tag]]] placeholderImage:nil options:SDWebImageProgressiveDownload completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
}];
NSLog productimg_array:
productimg_array (
"http://center.net/projects/View/images/img.png",
"http://center.net/projects/View/images/img1.png",
"http://center.net/projects/View/images/img2.png",
"http://center.net/projects/View/images/img3.png",
"http://center.net/projects/View/images/img4.png",
"http://center.net/projects/View/images/img5.png",
"http://center.net/projects/View/images/img6.png",
"http://center.net/projects/View/images/img7.png",
"http://center.net/projects/View/images/img8.png"
)
当我使用singleTap时,我没有从数据库值中获得正确的描述。这就是为什么我需要设置id而不是索引
- (void)singleTap:(UIGestureRecognizer *)singletap {
NSLog(@"singleTap1");
UIView *view = singletap.view;
switch (view.tag) {
case 0:
txtt=[[UITextView alloc]initWithFrame:CGRectMake(50, 100, 200, 100)];
txtt.text=[descript_array objectAtIndex:0];
txtt.editable=NO;
NSLog(@"text is %@",txtt.text);
[self.view addSubview:txtt];
break;
case 1:
txtt1=[[UITextView alloc]initWithFrame:CGRectMake(50, 100, 200, 100)];
txtt1.text=[descript_array objectAtIndex:1];
NSLog(@"text1 is %@",txtt1.text);
[self.view addSubview:txtt1];
break;
case 2:
txtt2=[[UITextView alloc]initWithFrame:CGRectMake(50, 100, 200, 100)];
txtt2.text=[descript_array objectAtIndex:2];
NSLog(@"text2 is %@",txtt2.text);
[self.view addSubview:txtt2];
break;
case 3:
txtt3=[[UITextView alloc]initWithFrame:CGRectMake(50, 100, 200, 100)];
txtt3.text=[descript_array objectAtIndex:3];
NSLog(@"text is %@",txtt3.text);
[self.view addSubview:txtt3];
break;
答案 0 :(得分:0)
删除解决问题的内部for循环
for (int i = 0; i<[productimg_array count]; i++ ) {
NSLog(@"productimg_array_index %@", productimg_array[i]);
imgView1=[[UIButton alloc]initWithFrame:CGRectMake(20+(i*74), 0, 72, 72)];
Width = Width + 20+(i*74);
//for (int i=0; i<[cat_imageidArray count]; i++){ **This line doesn't need**
NSLog(@"index %@", cat_imageidArray[i]);
tag=[[cat_imageidArray objectAtIndex:i]intValue];
**Changed**//imgView1.tag=tag;
imgView1.tag = i;
NSLog(@"tag is %d",tag);
//}
[imgView1 addTarget:self action:@selector(dbsofaClicked:) forControlEvents:UIControlEventTouchUpInside];
[imgView1 setImageWithURL:[NSURL URLWithString:[productimg_array objectAtIndex:i]]forState:UIControlStateNormal];
[scrollview addSubview:imgView1];
}