Swift / tableView使用instantiateViewControllerWithIdentifier传递数据

时间:2016-07-26 22:00:35

标签: ios swift backendless

我在tableView中填充了一组BackendlessUsers。如何在didSelectRowAtIndexPath函数中传递用户?

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

非常感谢帮助。

1 个答案:

答案 0 :(得分:2)

在DJProfileAsUserVC中创建一个 var用户:DJProfileAsUserVC!,之后创建一个 var用户:

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