2013-12-06 3 views
0

У меня есть строки URL изображения в базе данных. Затем я извлекаю изображения и добавляю их в productimg_array. Мне нужно показать изображения в ячейках таблицы. Я использовал SDWebImage для загрузки изображений из URL. Но только одно изображение, отображающее всю ячейку.Показать массив изображений в UITableView

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

     NSLog(@"productimg_arr is %@",productimg_array);   
    } 
} 

sqlite3_finalize(statement); 

вид таблицы:

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

Выход Вход:

productimg_arr is (
    "http://server.net/projects/View/images/img.png", 
    "http://server.net/projects/View/images/img1.png", 
    "http://server.net/projects/View/images/img2.png", 
    "http://server.net/projects/View/images/img3.png", 
    "http://server.net/projects/View/images/img4.png", 
    "http://server.net/projects/View/images/img5.png", 
    "http://server.net/projects/View/images/img6.png" 
) 
+0

Как я понял из вашего кода, вы пытались загрузить каждое изображение в 1 ячейку, потому что ваш «за» блок внутри метода, который возвращает 1 ячейку. Замените блок на objectAtIndex: indexPath.row. Я надеюсь, что это сработает :) –

ответ

0

ли это:

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

Тогда вы будете получать различные изображения во всех клетках.

+0

Это работает. Но изображения слишком малы. – user3073276

+0

Попробуйте увеличить высоту ячейки в методе heightForRowAtIndexPath. – Smita

+0

Вы также можете подклассифицировать UITableViewCell, чтобы выполнить настройку. –

0

Удалить цикл из cellForRoeAtIndexPath и использовать indexPath.row вместо I.

Заменить:

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

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

By:

[cell.imageView setImageWithURL:[NSURL URLWithString:[productimg_array objectAtIndex:indexPath.row]] 
       placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 
+0

Эй, я использую ленивую загрузку SDWebImage. Но проблема заключается только в img-изображении, показывающем все ячейки. [cell.imageView setImageWithURL: [NSURL URLWithString: [productimg_array objectAtIndex: i]] placeholderImage: [UIImage imageNamed: @ "placeholder.png"]]; – user3073276

+0

Я обновляю свой answer.check. – Smita

Смежные вопросы