David Seek David Seek - 4 months ago 51
iOS Question

Swift / tableView pass data with instantiateViewControllerWithIdentifier

I have an array of BackendlessUsers filled within a tableView. How can I pass the user within the didSelectRowAtIndexPath function?

var deejays: [BackendlessUser] = []

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

print("You selected cell #\(indexPath.row)!")
self.tableView.deselectRowAtIndexPath(indexPath, animated: true)

**let user = deejays[indexPath.row]**
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("DJProfileAsUserVC") as! DJProfileAsUserVC

dispatch_async(dispatch_get_main_queue()) {
self.presentViewController(vc, animated: true, completion: nil)
}
}


Help is very appreciated.

Answer

Create a var user: DJProfileAsUserVC! in DJProfileAsUserVC and after that to this:

let user = deejays[indexPath.row]
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("DJProfileAsUserVC") as! DJProfileAsUserVC
vc.user = user
Comments