0

我在数据库表中存储了一些图像。我正在使用 UIActionsheet 按钮来显示不同的图像集。当我单击 button1 时,它应该显示 button1 图像。但是,当我单击时,将显示所有图像,而不是显示特定行的图像。

代码:

类别表:

id cat_name

1   Imageset1
2   Imageset2
3   Imageset3

产品表:

id  cat_id   product_image
1    1         image1.png
2    1         image2.png
3    1         image3.png
4    2         nature1.png
5    2         nature2.png
6    3        animal.png

查询获取图片:

const char *sql = "SELECT id,cat_id,product_image FROM product where cat_id";

while (sqlite3_step(statement) == SQLITE_ROW) {


                catName = [[NSString alloc] initWithUTF8String:
                           (const char *) sqlite3_column_text(statement, 2)];
                NSLog(@"catName is %@",catName);

                [mArray addObject:catName];

}

点击的 UIActionSheet 按钮索引:

    -(void)actionSheetClickedButtonAtIndex:(int)buttonIndex {

        if (buttonIndex == 0) {
            NSLog(@"button");

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

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

}

    } else if (buttonIndex == 1) {

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

    [imgView2 setImageWithURL:[NSURL URLWithString:[mArray objectAtIndex:i]]
                                 forState:UIControlStateNormal];

}

    }

    }

这里 mArray 显示所有图像。仅具有 cat_id=1 图像的 Imageset1 的按钮索引 0。Imageset2 的按钮索引 1 具有 cat_id=2 图像。这些按钮的名称为类别表中的 cat_name。

4

1 回答 1

0

创建根据表中的类别返回图像数组的方法,并在操作表中使用此数组。以下是我的代码可能对您有帮助

-(NSMutableArray *)getImages:(int)index {

   char *st;

    NSMutableArray *arraylistget  = [[NSMutableArray alloc] init];

    st = sqlite3_mprintf("SELECT product_image FROM product Where cat_id=%d",index);

    sqlite3_stmt *statement;
    if (sqlite3_prepare_v2(rlraDb, st, -1, &statement, NULL) == SQLITE_OK) {

        // step through each row in the result set
        while (sqlite3_step(statement) == SQLITE_ROW) {


            const char* image = (const char*)sqlite3_column_text(statement, 0);

           /* NSMutableDictionary *dictObject = [[NSMutableDictionary alloc] init];

            [dictObject setValue:[NSString stringWithFormat:@"%@",image] forKey:@"ProfileImage"]; */

            [arraylistget addObject:image];

        }
        sqlite3_finalize(statement);
    }
    sqlite3_free(st);

    NSLog(@"arrScannerProfileIDs: %@",arraylistget);
    return arraylistget;
}


-(void)actionSheetClickedButtonAtIndex:(int)buttonIndex {
     if (buttonIndex == 0) {

          NSLog(@"button");

         mArray = [self getImages:1];

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

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

      } else if (buttonIndex == 1) {

             mArray = [self getImages:2];

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

             [imgView2 setImageWithURL:[NSURL URLWithString:[mArray objectAtIndex:i]]
                                     forState:UIControlStateNormal];

            }

      }

 }
于 2013-08-27T06:33:18.203 回答