0

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

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

8 Comments

on the line "let titleString = userObjects.objectAtIndex(indexPath!.count) as! String" it is saying "fatal error: unexpectedly found nil while unwrapping an optional value. I will keep playing around with it though.
@AllenSchuenke Is the titleString also coming from some element in the table view cell?
I am uploading it to dropbox and I will post the link for you, maybe it is my coding somewhere else. I am not sure to be honest
in the view controller that the info is being sent to, it has @IBOutlet weak var titleLabel: UILabel! var titleString = String() override func viewDidLoad() { super.viewDidLoad() self.titleLabel.text = self.titleString }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.