繁体   English   中英

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

[英]iOS how to load images in the cells of table view controller

我的应用程序包含一个根导航控制器,该控制器与具有自定义单元格的表视图控制器链接。 我可以使其从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>
...

将图像加载到表格视图单元中与将图像加载到其他位置没有什么不同。

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

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

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

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

您确定还要将图像视图添加到单元格的单元格中吗? 为确保确定,请检查代码中是否包含以下形式的某种形式的代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM