ios - 如何快速将图像添加到我的表格单元格?

标签 ios swift uitableview

我正在关注 this tutorial我正在为每个 json 条目创建单元格。没有照片一切正常:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("CELL")

    if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
    }

    let user:JSON =  JSON(self.items[indexPath.row])
   // print(user)
    let picURL = "/Users/K/Desktop/aproject/apps/address/addr/Images.xcassets/TabMap.imageset/up_dark.png"//just for tests - some random png
    let url = NSURL(string: picURL)
    let data = NSData(contentsOfURL: url!)

    cell!.textLabel?.text = user["description"].string
 //   cell?.imageView?.image = UIImage(data: data!)

    return cell!
}

,但是当我添加照片时(取消注释这一行):

cell?.imageView?.image = UIImage(data: data!)

然后我得到错误:

enter image description here

fatal error: unexpectedly found nil while unwrapping an Optional value

我的计划是传递一个 url 而不是硬编码的照片(因此使用 user["photo"] 而不是链接),但为了测试,我将 url 粘贴到我的自定义图片。

如何将它添加到文本旁边?

最佳答案

对于本 map 片:

cell!.imageView?.image = UIImage(named:"ImageName")

关于ios - 如何快速将图像添加到我的表格单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35531441/

相关文章:

ios - 当用户向下滚动 75% 时在 uiscrollview 中自动回发

ios - xCode 4.3.2 - 构建分析,如何运行该应用程序?

ios - UITableView中的UITextView,使用AutoLayout平滑展开

swift - 从 UITableView 中删除行时出错

string - 两个字符串具有相同的语言含义是什么意思?

swift - 如果结构项等于 nil

ios - Brain Tree 体系结构的 undefined symbol x86_64 : "_OBJC_CLASS_$_Braintree"

ios - 以编程方式更改启动画面图像

swift - 无法查看 iOS 10 本地通知的操作按钮

ios - 如何延迟 iOS 中 UISlider 的开始触摸事件?