【发布时间】:2016-04-16 04:08:22
【问题描述】:
当我想在 m 单元格中设置图片时,我的 TableViewController 中有一个奇怪的情况。我的单元格是:
类 TableViewCell
class TableViewCell: UITableViewCell {
/* OUTLETS */
@IBOutlet weak var pictureInRowOutlet: UIImageView!
@IBOutlet weak var postDataOutlet: UILabel!
@IBOutlet weak var titleOutlet: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization cod
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}//end of class
类 MyTableView
class MyTableView: UITableViewController{
var data:[[String:String]]? // data load from serwer
//code we don't need
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell
// now I want to set up picture in row
let urlAdressPhoto = (data?[indexPath.item]["picture"])!
// here, when I print urlAdressPhote I see correct value as String
cell.pictureInRowOutlet.image = UIImage(data: NSData(contentsOfURL: NSURL(string: urlAdressPhoto)!)!)
// in this moment I recive an information about "unexpectedly found nil while unwrapping an Optional value".
return cell
}
谁能告诉我为什么以及如何解决这个问题?
【问题讨论】:
-
NSData(contentsOfURL: NSURL(string: urlAdressPhoto)!)!您强制解开两个选项 - 这会导致崩溃。修复它。 -
但是当我在任何情况下尝试删除
!时,Xcode 都会收到错误提示 -
阅读可选项以及如何有条件地打开它们。
标签: ios swift uiimage tableview nsurl