iOS如何在表视图控制器的单元格中加载图像

时间:2013-12-02 19:00:14

标签: ios objective-c uitableview

我的应用包含一个根导航控制器,该控制器链接到带有自定义单元格的表视图控制器。我可以将plist中的值加载到标签中,但它不适用于图像。谢谢你的建议。

cell.chapterImage.image = [[ContentArray objectAtIndex:indexPath.row] objectForKey:@"ic_menu"];

cell.Chapter.text = [[ContentArray objectAtIndex:indexPath.row] objectForKey:@"name"];

plist中:

<plist version="1.0">
<array>
<dict>
    <key>nat_num</key>
    <string>1</string>
    <key>name</key>
    <string>Sample String</string>
    <key>ic_menu</key>
    <string>main1@2x.png</string>
</dict>
...

3 个答案:

答案 0 :(得分:1)

将图像加载到表格视图单元格与在其他地方加载图像没有区别。

cell.chapterImage.image = [UIImage imageNamed:
    [[ContentArray objectAtIndex:indexPath.row] objectForKey:@"ic_menu"]];

您要做的是将字符串值放入图像中。这适用于cell.Chapter.text,因为cell.Chapter.text期望为其分配字符串。同时,cell.chapterImage.image期望为其分配一个图像,因此我们必须使用[UIImage imageNamed:]这是UIImage的工厂方法,它返回与您指定的文件名关联的图像对象。字符串参数。

答案 1 :(得分:1)

您可以使用以下代码获取图片:

cell.chapterImage.image = [UIImage imageNamed:[[ContentArray objectAtIndex:indexPath.row] objectForKey:@"ic_menu"]];

答案 2 :(得分:1)

您确定要将图像视图添加到单元格的ciew中吗?为了确保这一点,请检查代码中是否存在以下代码段的某种形式:

cell.chapterImage.image = [UIImage imageNamed:[[ContentArray objectAtIndex:indexPath.row] objectForKey:@"ic_menu"]];
[cell addSubview:cell.chapterImage];

此外,尝试在表格视图委托willDisplayCell

内调整图像大小和位置
- (void)tableView:(UITableView *)tableView willDisplayCell:(LWLibraryListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.chapterImage.frame = CGRectMake(0, 0, 20, 20);
}