I seem to be running into an issue and don't know where I am going wrong. I am trying to send the user's post to another view controller through a button, and my app keeps crashing. Below is the code for the tableview cell and the the prepare for segue. I am still new to the app development world. So I am sorry if my code is a little messy. Also I am using Parse as my backend. I don't know if that makes a difference.
thank you in advanced!
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TimelineTableViewCell
let userPost: PFObject = self.userObjects.objectAtIndex(indexPath.row) as! PFObject
cell.backgroundColor = UIColor.darkGrayColor().colorWithAlphaComponent(0.2)
cell.layer.borderColor = UIColor.lightGrayColor().CGColor
cell.layer.borderWidth = 0.5
cell.reportPost.tag = indexPath.row
cell.reportPost.addTarget(self, action: "buttonAction", forControlEvents: .TouchUpInside)
// Display Users Post's
var userText = userPost["text"] as? String
cell.TextLabel!.text = userText
// Display Date and Time
var Date: NSDateFormatter = NSDateFormatter()
Date.dateFormat = "MM-dd HH:mm"
cell.dateLabel.text = Date.stringFromDate(userPost.createdAt!)
// Display username
var findUser:PFQuery = PFUser.query()!
let Id = userPost.objectForKey("username")?.objectId as NSString!
findUser.whereKey("objectId", equalTo: Id)
findUser.findObjectsInBackgroundWithBlock{
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil{
var user:PFUser = (objects as NSArray!).lastObject as! PFUser
cell.userLabel.text = user.username
// Getting User's Profile Image
var initialThumbnail = UIImage(named: "PAHS_Logo")
cell.userImage.image = initialThumbnail
if let PhotoFile = user["ProfileImage"] as? PFFile {
PhotoFile.getDataInBackgroundWithBlock{
(ImageData:NSData?, error:NSError?)->Void in
if error == nil{
let Image:UIImage = UIImage(data: ImageData!)!
cell.userImage.image = Image
}else{
cell.userImage.image = initialThumbnail
}
}
}
}
}
return cell
}
func buttonAction(sender: UIButton!){
let titleString = self.userObjects.objectAtIndex(sender.tag) as? String
let firstActivityItem = "\(titleString)"
let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: [firstActivityItem], applicationActivities: nil)
self.presentViewController(activityViewController, animated: true, completion: nil)
}
var valueToPass: String!
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let indexPath = tableView.indexPathForSelectedRow();
let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!;
valueToPass = currentCell.textLabel?.text
performSegueWithIdentifier("reportPost", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "reportPost")
{
var viewController: reportViewController = segue.destinationViewController as! reportViewController
let indexPath = self.tableView!.indexPathsForSelectedRows();
let titleString = userObjects.objectAtIndex(indexPath!.count) as! String
viewController.titleString = titleString
viewController.titleLabel.text = valueToPass //fatal error: unexpectedly found nil while unwrapping an Optional value
self.presentViewController(viewController, animated: true, completion: nil)
}
}
You can change performSegueWithIdentifier("reportPost", sender: self)
to performSegueWithIdentifier("reportPost", sender: indexPath)
Then, in prepareForSegue()
, the sender
is going to be the indexPath. You can change your prepareForSegue()
to this:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "reportPost")
{
var viewController: reportViewController = segue.destinationViewController as! reportViewController
let indexPath = sender as! NSIndexPath
let titleString = userObjects[indexPath.row] as! String // I am assuming userObjects is the object containing the data you want
viewController.titleString = titleString
let cell = tableView.cellForRowAtIndexPath(indexPath) as! UITableViewCell
let valueToPass = cell.textLabel?.text
viewController.titleLabel.text = valueToPass
self.presentViewController(viewController, animated: true, completion: nil)
}
}
If you do it this way, you can get rid of var valueToPass: String!
above tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.