2016-03-10 51 views
0

我想在PFTableViewCell中使用Parse和Swift 2顯示一些文本和圖像。我能夠完全顯示單元格內的文本,但是當我添加代碼圖像顯示和運行應用程序,它給了我下面的錯誤:「致命錯誤:意外發現零而展開的可選值」 這裏是我使用的代碼:我如何添加圖像到PFTableViewCell Swift 2 Xcode 7

 var textArray = [String]() 
var imageArray = [UIImage]() 



@IBOutlet weak var tableView2: UITableView! 




override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view, typically from a nib. 

    let shopList = PFQuery(className:"Shops") 
    let runkey = shopList.orderByAscending("shopName") 

    runkey.findObjectsInBackgroundWithBlock {(objects: [PFObject]?, error: NSError?) -> Void in 


     if error == nil { 
      if let objects = objects as [PFObject]! { 
       for object in objects { 
        let loadTxt = object.objectForKey("shopName") as! String 
        let loadImg = object.objectForKey("shopImages") as! UIImage 

        self.imageArray.append(loadImg) 
        self.textArray.append(loadTxt) 


        print(self.textArray) 


       } 
      } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell 

    cell.labelShopName?.text = textArray[indexPath.row] 
    cell.shopImages?.image = imageArray[indexPath.row] 




    return cell 
} 

回答

0

我需要要知道錯誤發生在哪一行。我的猜測是,由於某種原因,你試圖拉取的數據=零。要檢查是否有任何內容正在返回,請註釋代碼失敗的行並執行「print()」。如果返回nil,則必須嘗試其他方法,因爲圖像未被下載。