设置数据库中的索引值数组

时间:2022-06-01 20:13:46

I have id value and product_image value in database. Image is associated with id value. I'm setting productimg_array image values to buttons for click. SO, here i need to set the id value to this button.So, i'm uisng for loop. If i use int i=0 the images are displaying, but if i set int i= cat_iamgeId the images are not displaying. Why? How to assign the id value as a index

我在数据库中有id值和product_image值。图像与id值相关联。我将productimg_array图像值设置为按钮以进行单击。所以,这里我需要将id值设置为这个按钮。所以,我是循环的uisng。如果我使用int i = 0,则显示图像,但如果我设置int i = cat_iamgeId则图像不显示。为什么?如何将id值指定为索引

-(void)actionSheetClickedButtonAtIndex:(int)buttonIndex {


    if (buttonIndex >=0 && buttonIndex < [categoryIds count]) {

        NSInteger categoryId = [categoryIds[buttonIndex] intValue];


      NSLog(@"button is %d",buttonIndex);

        NSLog(@"categoryId is %ld", (long)categoryId);

        productimg_array = [[NSMutableArray array]init];

        descript_array = [[NSMutableArray alloc]init] ;

        cat_imageidArray = [[NSMutableArray alloc]init];


        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];






                descript = [[NSString alloc] initWithUTF8String:
                            (const char *) sqlite3_column_text(statement, 4)];

             //   NSLog(@"descript is %@",descript);


                [descript_array addObject:descript];



           }


        }

For loop for UIButton click:

对于UIButton的循环点击:

    for (int i = 0; i<[productimg_array count]; i++ ) {






       NSLog(@"productimg_array_index %@", cat_imageidArray[i]);





            imgView1=[[UIButton alloc]initWithFrame:CGRectMake(20+(i*74), 0, 72, 72)];

            Width = Width + 20+(i*74);




              imgView1.tag = i;

              NSLog(@"product id is %ld ", (long)imgView1.tag);


            [imgView1 addTarget:self action:@selector(dbsofaClicked:) forControlEvents:UIControlEventTouchUpInside];


            [imgView1 setImageWithURL:[NSURL URLWithString:[productimg_array objectAtIndex:i]]
                             forState:UIControlStateNormal];



            [scrollview addSubview:imgView1];






        }

NSlog tag value:

NSlog标记值:

product id is 202 
 product id is 203 
 product id is 204 
 product id is 205 
 clicked
 button 202 is clicked.
 *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 202 beyond bounds [0 .. 3]'

imgView1 click: (dbsofaclicked:)

imgView1点击:(dbsofaclicked :)

-(void)dbsofaClicked:(id)sender{


   NSLog(@"clicked");


    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) {



    }];

    [mmageView setTag:[sender tag]];

    [self.view addSubview:mmageView];
}

1 个解决方案

#1


1  

There is a much better way to do this. Use one array to hold the data for each product image.

有一个更好的方法来做到这一点。使用一个数组来保存每个产品图像的数据。

NSMutableArray *image_array = [NSMutableArray array];

while (sqlite3_step(statement) == SQLITE_ROW) {
    cat_imageId = sqlite3_column_int(statement, 0);
    product_image = [[NSString alloc] initWithUTF8String:
                     (const char *) sqlite3_column_text(statement, 2)];
    [image_array addObject:@{ "id" : @(cat_imageId), @"url" : product_image }];
    NSLog(@"cat_imageId is %ld",(long)cat_imageId);
    NSLog(@"product_image is %@",product_image);
}

This creates an array of dictionaries. This keeps the data for each image together. This allows you to sort the data if needed and you only have one array to deal with.

这会创建一个字典数组。这样可以将每个图像的数据保存在一起。这允许您根据需要对数据进行排序,并且只有一个数组可以处理。

Then your code to create the buttons becomes (based on your update):

然后您创建按钮的代码变为(基于您的更新):

for (NSUInteger i = 0; i < image_array.count; i++) {
    NSDictionary *image_data = image_array[i];

    UIButton *imgView1 = [[UIButton alloc] initWithFrame:CGRectMake(20+(i*74), 0, 72, 72)];
    Width = Width + 20 + (i * 74);

    imgView1.tag = i;
    [imgView1 addTarget:self action:@selector(dbsofaClicked:) forControlEvents:UIControlEventTouchUpInside];
    [imgView1 setImageWithURL:[NSURL URLWithString:image_data[@"url"]] forState:UIControlStateNormal];

    [scrollview addSubview:imgView1];
}

#1


1  

There is a much better way to do this. Use one array to hold the data for each product image.

有一个更好的方法来做到这一点。使用一个数组来保存每个产品图像的数据。

NSMutableArray *image_array = [NSMutableArray array];

while (sqlite3_step(statement) == SQLITE_ROW) {
    cat_imageId = sqlite3_column_int(statement, 0);
    product_image = [[NSString alloc] initWithUTF8String:
                     (const char *) sqlite3_column_text(statement, 2)];
    [image_array addObject:@{ "id" : @(cat_imageId), @"url" : product_image }];
    NSLog(@"cat_imageId is %ld",(long)cat_imageId);
    NSLog(@"product_image is %@",product_image);
}

This creates an array of dictionaries. This keeps the data for each image together. This allows you to sort the data if needed and you only have one array to deal with.

这会创建一个字典数组。这样可以将每个图像的数据保存在一起。这允许您根据需要对数据进行排序,并且只有一个数组可以处理。

Then your code to create the buttons becomes (based on your update):

然后您创建按钮的代码变为(基于您的更新):

for (NSUInteger i = 0; i < image_array.count; i++) {
    NSDictionary *image_data = image_array[i];

    UIButton *imgView1 = [[UIButton alloc] initWithFrame:CGRectMake(20+(i*74), 0, 72, 72)];
    Width = Width + 20 + (i * 74);

    imgView1.tag = i;
    [imgView1 addTarget:self action:@selector(dbsofaClicked:) forControlEvents:UIControlEventTouchUpInside];
    [imgView1 setImageWithURL:[NSURL URLWithString:image_data[@"url"]] forState:UIControlStateNormal];

    [scrollview addSubview:imgView1];
}