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

时间:2013-12-12 03:46:53

标签: ios sqlite uibutton indexing nsmutablearray

我在数据库中有id值和product_image值。图像与id值相关联。我将productimg_array图像值设置为按钮以进行单击。所以,我需要在这个按钮上设置id值。所以,我是循环的。如果我使用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];



           }


        }

对于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标记值:

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点击:(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 个答案:

答案 0 :(得分:1)

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

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);
}

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

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

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