ios - 单元格点击时显示详细 View Swift Parse

标签 ios swift segue

正如标题所说,我试图在点击单元格时显示详细信息 View 。我的问题是我正在使用的 prepareForSegue 函数。我没有得到任何错误,但是当我在模拟器上运行时,我得到了一个未捕获的异常。它出现在 prepareForSegue 函数的最后一行。它发现名称为 nil,我不确定为什么变量为 nil。任何帮助将不胜感激。

更新: Uncaught Error 不再是问题。现在点击单元格时不会显示数据。

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]
        }
    }
}
}

最佳答案

您应该将字符串传递给下一个 Controller ,并将此文本设置为下一个屏幕的 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

关于ios - 单元格点击时显示详细 View Swift Parse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34190651/

相关文章:

iphone - iOS OTA 安装未注册自定义 URL 方案

ios - 自调整 TableView 单元格不起作用

javascript - 是否可以在 React Native 中将 configureScene 的 PushFromRight 更改或添加到 PushFromLeft?

ios - 我想创建一个标签来跟踪访问 View Controller 的次数

ios - 我如何解除 segue 回退?

ios - 未调用 NSFectchedResultController 委托(delegate)?

ios - 如何快速截取 UIView 的屏幕截图?

ios - UIImagePickerController - 默认情况下如何访问前置摄像头?

swift - 如何在函数中设置静态变量的属性

ios - 在 RestKit 中成功查找后获取 Null 值