2015-05-28 70 views
0

我的tableview中的每一行都有一個postID。我想獲得這個ID併發送到另一個視圖。TableView:如何從選定的行中獲取字符串?

我該怎麼做?

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

回答

4

使用的數據模型,而不是細胞

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

感謝北斗星,你真了不起:) –

+0

沒問題:-)不要忘記,以紀念該解決您的問題的答案;-) – Wain

相關問題