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