2015-12-09 71 views
0

因此,標題說我試圖在單元格被點擊時顯示細節視圖。我的問題是我正在使用的prepareForSegue函數。我沒有得到任何錯誤,但是當我在模擬器上運行時,我得到一個未捕獲的異常。它發生在prepareForSegue函數的最後一行。它的名稱爲零,我不知道爲什麼變量爲零。任何幫助將非常感激。當單元格點擊時顯示細節視圖Swift分析

更新:未被捕獲的錯誤不再是一個問題。現在當單元格被點擊時數據不會被顯示。

class LocalPostsTableViewController: UITableViewController, UINavigationBarDelegate { 

var currentIndexPath: NSIndexPath? 

func displayAlert(title: String, message: String) { 
    var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert) 
    alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in 
    })) 
    self.presentViewController(alert, animated: true, completion: nil) 
} 

var names = [String]() 
var locations = [String]() 
var dates = [String]() 
var imageFiles = [PFFile]() 
var abouts = [String]() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    PFGeoPoint.geoPointForCurrentLocationInBackground { (geoPoint, error) -> Void in 
     if let geoPoint = geoPoint { 
      PFUser.currentUser()?["location"] = geoPoint 
      PFUser.currentUser()?.saveInBackground() 

    var getLocalPostsQuery = PFQuery(className: "publicPosts") 
    if let latitude = PFUser.currentUser()!["location"].latitude { 
     if let longitude = PFUser.currentUser()!["location"].longitude { 
    getLocalPostsQuery.whereKey("searchLocation", withinGeoBoxFromSouthwest: PFGeoPoint(latitude: latitude - 0.5, longitude: longitude - 0.5), toNortheast: PFGeoPoint(latitude: latitude + 0.5, longitude: longitude + 0.5)) 
    getLocalPostsQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in 
     if let objects = objects { 

       self.names.removeAll(keepCapacity: true) 
       self.locations.removeAll(keepCapacity: true) 
       self.abouts.removeAll(keepCapacity: true) 
       self.dates.removeAll(keepCapacity: true) 
       self.imageFiles.removeAll(keepCapacity: true) 

       for object in objects { 

        self.names.append(object["name"] as! String) 
        self.locations.append(object["location"] as! String) 
        self.dates.append(object["date"] as! String) 
        self.abouts.append(object["about"] as! String) 
        self.imageFiles.append(object["imageFile"] as! PFFile) 
        self.tableView.reloadData() 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

// MARK: - Table view data source 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
} 

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


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let LECell = tableView.dequeueReusableCellWithIdentifier("LocalPostsCell", forIndexPath: indexPath) as! LocalPostsTableViewCell 

    imageFiles[indexPath.row].getDataInBackgroundWithBlock { (data, error) -> Void in 
     if let downloadedImage = UIImage(data: data!) { 

      LECell.postImage.image = downloadedImage 
     } 
    } 


    LECell.postName.text = names[indexPath.row] 
    LECell.postLocation.text = locations[indexPath.row] 
    LECell.postDate.text = dates[indexPath.row] 

    return LECell 
} 

let localPostsDetailSegue = "showLocalPostsDetailView" 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == localPostsDetailSegue { 
     let detailScene = segue.destinationViewController as! LocalPostsDetailViewController 

     if let indexPath = self.tableView.indexPathForSelectedRow { 
      let row = Int(indexPath.row) 
      detailScene.postName?.text = names[row] 
     } 
    } 
} 
} 

回答

1

你應該只傳遞字符串到一個控制器,並設置這段文字在下一屏的viewDidLoad標記。

定義:

var postName: String! 

LocalPostsDetailViewController

prepareForSegue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
if segue.identifier == localPostsDetailSegue { 
    let detailScene = segue.destinationViewController as! LocalPostsDetailViewController 

    if let indexPath = self.tableView.indexPathForSelectedRow { 
     let row = Int(indexPath.row) 
     detailScene.postName = names[row] 
    } 
} 

}

LocalPostsDetailViewControllerviewDidLoad設置它:

self.postName?.text = postName 
+0

嘗試,但是當我做detailScene.postName?.text = postName 它說detailScene是一個未解決的標識符 – m1234

+0

對不起,我把名字是'postname'不是'postName'所以它錯誤。請更改並重試。 –

+0

仍然使用了未解決的標識符detailScene – m1234

0

沒有必要使用didSelectRowAtIndexPath。就在與標識符故事板定義SEGUE和下面應該工作:

let localPostsDetailSegue = "showLocalPostsDetailView" 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     if segue.identifier == localPostsDetailSegue { 
      let detailScene = segue.destinationViewController as! LocalPostsDetailViewController 

      if let indexPath = self.tableView.indexPathForSelectedRow { 
       let row = Int(indexPath.row) 
       detailScene.postName?.text = names[row] 
      } 
     } 
    } 
+0

謝謝你我我的代碼更新到你的答案,我仍然得到錯誤它找到零展開的可選值時:detailScene.postName.text! = names [row] – m1234

+0

tableView是否加載正確的數據?只有當你點擊單元格時纔會發生錯誤? – adolfosrs

+0

當我點擊單元格並導致崩潰 – m1234

相關問題