【问题标题】:Show image in my Prototype Cells xcode在我的原型单元 xcode 中显示图像
【发布时间】:2014-11-02 19:49:27
【问题描述】:

下午好,

我在原型单元格中显示图像时遇到问题。目前我只能显示标签文本,我想知道我必须做什么才能显示图像。

这是当前进程的截图:

我有图片的 URL,但目前只显示为标签:

http://i.stack.imgur.com/P8l5t.png

我想将其显示为图像。我必须做什么才能实现这一目标?

ViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Retrieve cell
    NSString *cellIdentifier = @"BasicCell";
    UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    // Get the location to be shown
    Location *item = _feedItems[indexPath.row];

    // Get references to labels of cell
    myCell.textLabel.text = item.address;

    return myCell;
}

提前致谢。

【问题讨论】:

  • 但是您甚至没有尝试在此处添加图像...您可以将图像添加为子视图或使用单元格的图像属性。
  • 我在 MainStoryBoard 中添加了一个 imageView。
  • 但是在你的 cellForRowAtIndexPath 中,你只是设置了你的单元格的文本标签属性...item.address 是图片的网址吗?
  • 是的,item.address就是图片的url路径。如何设置单元格的图像属性?谢谢林赛。
  • 图片 url 在网站中,完整的 url 路径。谢谢。

标签: javascript ios xcode


【解决方案1】:

您可以使用UITableViewimageView 对象,而不是UITableViewUILabel 对象。例如,替换:

myCell.textLabel.text = item.address;

与:

myCell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:item.address]]];

或者您可以通过添加UIImageView 作为子视图来自定义图像视图。

【讨论】:

  • 我收到以下错误:/Users/Downloads/DatabaseTableViewApp 3/DatabaseTableViewApp/ViewController.m:92:83:不兼容的指针类型将“NSString *”发送到“NSURL *”类型的参数谢谢。
  • @JulienKP 哦,那么如果您的地址被格式化为 NSString,则必须将其更改为 NSUrl。我已经更新了我的答案。
【解决方案2】:

return myCell 之前设置单元格的图像属性。例如,如果您有一个名为“images”的图像数组对应于您的单元格,您可以执行以下操作:

myCell.imageView.image = images[indexPath.row]

您还可以通过子类化UITableViewCell 并将表格视图中原型单元的类设置为您自己的子类来创建自己的原型单元。然后,您可以在原型单元格中的任意位置添加您自己的UIImageView,并创建一个链接到您的自定义单元格类的IBOutlet。你的方法可能看起来像这样:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Retrieve cell
    NSString *cellIdentifier = @"BasicCell";
    MyCustomCell *customCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    // Get the location to be shown
    Location *item = _feedItems[indexPath.row];

    // Get references to labels of cell
    customCell.customLabel.text = item.address;

    customCell.customImageView.image = arrayOfImages[indexPath.row]

    return customCell;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    相关资源
    最近更新 更多