获取特定的数据行

时间:2013-08-27 05:51:30

标签: iphone sqlite uibutton nsarray

我在数据库表中存储了一些图像。我正在使用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显示所有图像。按钮索引0仅适用于具有cat_id = 1图像的Imageset1。按钮索引1具有具有cat_id = 2个图像的Imageset2。这些按钮的类别名称为cat_name。

1 个答案:

答案 0 :(得分: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];

            }

      }

 }