TableView:如何从选定的行中获取字符串?

时间:2015-05-28 11:30:17

标签: ios uitableview swift

我的tableview中的每一行都有一个postID。我希望获得此ID并发送到另一个视图。

我该怎么做?

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

1 个答案:

答案 0 :(得分: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
}