【发布时间】:2013-12-04 05:06:49
【问题描述】:
我在数据库中有产品表。产品表我有 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 而不是 index
- (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;
【问题讨论】:
-
db 和 productimg_array 一切正常。只有你的 imgView1 按钮标签比你的 productimg_array 具有更大的价值。
-
您必须采用包含 Product_id 和 cat_id 的类,而不是采用两个数组。您编写了错误的逻辑并实现了错误的数据结构。所以,首先请解决那个问题。它崩溃是因为你使用了错误的机制。
标签: ios sqlite tags uibutton nsmutablearray