我在数据库中有图像URL字符串。然后我检索图像并添加到productimg_array。我需要将productimg_array图像显示给UITableView cell
。我正在使用imageView.image = [UIImage imageNamed:[productimg_array objectAtIndex:indexPath.row]];
但是app崩溃了。
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;
}
答案 0 :(得分:2)
cell.imageView.image = [productimg_array objectAtIndex:indexPath.row];
这里cell.imageView.image
期望图像在位置,但数组有一组字符串,您要传递给在imageview中设置
cell.imageView.image =[UIImage imageNamed:[productimg_array objectAtIndex:indexPath.row]];
注意:
product_image = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
如果product_image
为空或者db中为null,则会崩溃,所以这样做
char *nameChars = (char *) sqlite3_column_text(statement, 2);
if (nameChars) {
product_image = [[NSString alloc] initWithUTF8String:nameChars];
}
答案 1 :(得分:0)
首先,您需要从URL位置下载图像,然后必须将其分配给图像视图。为此,您可以使用延迟加载。