作为Xcode初学者,我想在我的应用程序的表格单元格中显示缩略图。现在,我有这个代码解析JSON数据并将其发布在单元格的标题和副标题中。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
}
cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"receta"];
cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"koha"];
return cell;
}
如何在单元格右侧显示缩略图?
感谢。
答案 0 :(得分:3)
如果您不想创建自定义单元格,可以创建一个包含所需图像的UIImageView
,并将其设置为单元格的accessoryView
,这将显示在单元格的右边缘。细胞。您还需要确保单元格的高度足够高,以适应图像的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
- (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];
}
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"some-image-name"]];
return cell;
}
如果图片总是固定大小,您可以返回常量(我选择100
),或者您可以查看UIImage
的{{1}}并返回size
之类的内容1}}用于一些额外的填充。