简体   繁体   中英

TableView: how to get a string from selected row?

Each row in my tableview have a postID. And I want to get this id and send to another View.

How can I do that?

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! PostsTableViewCell
    //get posts data
    var rowData: NSDictionary = Posts[indexPath.row] as! NSDictionary
    //get title
    let title:String = rowData["post_title"] as! String
    cell.posttitle.text = title
    //get categorty
    let category:String = rowData["category_name"] as! String
    cell.postcategory.text = category
    //get image
    let imgurl = rowData["post_feature_image"] as! String
    let realurl = root + imgurl
    var imgURL: NSURL = NSURL(string: realurl)!
    let imgData = NSData(contentsOfURL: imgURL)
    cell.fimg.image = UIImage(data: imgData!)
    // get post iD
    var postID:String = rowData["post_id"] as! String
    return cell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let ccell = tableView.cellForRowAtIndexPath(indexPath)
    let cid:String = ccell?.contentMode.rawValue(String: postID)
}

Use the data model, not the cell

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var rowData: NSDictionary = Posts[indexPath.row] as! NSDictionary
    let cid:String = rowData["post_id"] as! String
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM