我正在构建一个应用程序,它将在表格 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
}
}
最佳答案
好吧,这就是问题所在。您有两个 socket 连接,图像和图片。删除“image”,因为在您的代码中您使用的是“pic”。 当您有多个连接时,Xcode 会感到困惑。 希望对您有帮助!
附注要删除“图像”连接,只需点击旁边的“X”按钮即可。
关于swift - 尝试在 TableView 中显示图像时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38361186/