swift - 尝试在 TableView 中显示图像时出错

标签 swift uitableview uiimageview

我正在构建一个应用程序,它将在表格 View 中列出一堆名人。它将包括他们的图像和 Twitter 用户名。我在编辑器中没有收到任何错误,但是当我运行我的应用程序时,我收到以下消息:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView _isResizable]: unrecognized selector sent to instance

我不确定为什么会收到此消息以及我可以采取哪些措施来解决它...

这是我的 ViewController 中的代码:

class CelebListViewController: UITableViewController {

    let celebs = Celeb.celebsFromBundle()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return celebs.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell: CelebListViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CelebListViewCell

        //Keep
        let celeb = celebs[indexPath.row]

        cell.setCell(celeb.twitter, imageName: celeb.imageName)

        return cell

    }

}

这是我的 ViewCell 中的代码:

class CelebListViewCell: UITableViewCell {

    @IBOutlet weak var pic: UIImageView!

    @IBOutlet weak var handle: UILabel!

    func setCell(handleText: String, imageName: String) {
        self.handle.text = handleText
        self.pic.image = UIImage(named: imageName)
    }

}

我觉得这个问题是由我在某处使用 UIImage 而不是 UIImageView 引起的,但我找不到它。

无论如何,感谢您的帮助。

附注我有一个名人对象,我正在其中设置所有数据。这是以防万一的代码:

struct Celeb {
    let name: String
    let twitter: String
    let imageName: String
    let image: UIImage

    init(name: String, twitter: String, image: UIImage, imageName: String) {
        self.name = name
        self.twitter = twitter
        self.image = image
        self.imageName = imageName
    }

    static func celebsFromBundle() -> [Celeb] {

        var celebs = [Celeb]()

        guard let path = NSBundle.mainBundle().pathForResource("celebs", ofType: "json"),
            data = NSData(contentsOfFile: path) else {
                return celebs
        }

        do {
            let rootObject = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)

            guard let celebObjects = rootObject["celebs"] as? [[String: AnyObject]] else {
                return celebs
            }

            for celebObject in celebObjects {
                if let name = celebObject["name"] as? String,
                    twitter = celebObject["twitter"]  as? String,
                    imageName = celebObject["image"] as? String,
                    image = UIImage(named: imageName) {

                    let celeb = Celeb(name: name, twitter: twitter, image: image, imageName: imageName)
                    celebs.append(celeb)

                }
            }

        } catch {
            return celebs
        }

        return celebs
    }

}

这也是我的 Storyboard文档大纲(如果有帮助的话): enter image description here

图像连接: enter image description here

最佳答案

好吧,这就是问题所在。您有两个 socket 连接,图像和图片。删除“image”,因为在您的代码中您使用的是“pic”。 当您有多个连接时,Xcode 会感到困惑。 希望对您有帮助!

附注要删除“图像”连接,只需点击旁边的“X”按钮即可。

关于swift - 尝试在 TableView 中显示图像时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38361186/

相关文章:

ios - 蒙面的 UIVisualEffectView 在 iOS 10 上不起作用

iphone - 是否可以在不使用 UITableViewDelegate 的情况下调整 UITableViewCell 的大小?

ios - 为什么 -drawRect 比对 UITableViews 使用 CALayers/UIViews 更快?

ios - 为 UIImageView 禁用大型 UIImage

ios - 下载的图片需要很长时间才能显示在 UIImageView 上

iOS ImageView : how can I implement this?

ios - readwrite 和 readonly 互斥

ios - 在 UIAlert 之后执行 Segue

xcode - Swift:将自定义类分配给所有 UINavigationController 是否正确?

ios - 什么时候设置更改 heightForRowAt UITableView 动画被打破