-1

I have image URL strings in database. Then I retrieving the images and add to productimg_array. I need to show the productimg_array images to UITableView cell. I'm using imageView.image=[UIImage imageNamed:[productimg_array objectAtIndex:indexPath.row]]; But app crashing.

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

        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.



            while (sqlite3_step(statement) == SQLITE_ROW) {


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

                [productimg_array addObject:product_image];


         }
        }

TableView:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSLog(@"productimg_array is %lu",(unsigned long)[productimg_array count]);

    return [productimg_array count];


}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }


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

[cell.imageView setImageWithURL:[NSURL URLWithString:[productimg_array objectAtIndex:i]]
               placeholderImage:[UIImage imageNamed:@"placeholder.png"]];


}

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;

}
user3073276
  • 74
  • 1
  • 2
  • 13

2 Answers2

2
cell.imageView.image = [productimg_array objectAtIndex:indexPath.row];

here cell.imageView.image expect an image in the position but the array has a set of strings that you are passing to set in the imageview

 cell.imageView.image =[UIImage imageNamed:[productimg_array objectAtIndex:indexPath.row]];

Note :

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

can crash if the product_image is empty or null in db,so do this

char *nameChars = (char *) sqlite3_column_text(statement, 2);
if (nameChars) {
   product_image = [[NSString alloc] initWithUTF8String:nameChars];
}
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
0

First you need to download that images from URL location and then you have to assign it to your image view.For this you can use lazy loading.

iSmita
  • 1,292
  • 1
  • 9
  • 24