从VC中获取依赖于单元格的var以用于UIActivityViewController(Swift)

时间:2014-09-18 00:46:51

标签: ios json uitableview swift uiactivityviewcontroller

我的UITableView个单元格中填充了JSON。我的一个关键值是“ID”,在我的UIActivityViewController中构建网址是必要的。

我没有使用didSelectRowAtIndexPath,而是在每个单元格中都有UIButton来激活ActivityViewController。如何检测选择了哪个单元格的共享按钮,并将该单元格的“ID”发送到ActivityViewController,以便我可以将其附加到我的网址?

这是我的VC的字符串,可以用“ID”填充。

var passID: String = ""

这是我的IBActionUIActivityViewController设置。我的“secondActivityItem”是URL,只取决于单元格的“ID”。

@IBAction func shareSheet(sender: AnyObject){

let firstActivityItem = "Help us meet a need this Christmas season! Can you meet this need?"
let secondActivityItem : NSURL = NSURL.URLWithString("http://www.domain.com/\(passID)")
let activityViewController : UIActivityViewController = UIActivityViewController(
    activityItems: [firstActivityItem, secondActivityItem], applicationActivities: nil)

activityViewController.completionWithItemsHandler = {(activityType, completed:Bool, returnedItems:Array!, error:NSError!) in

    if !completed {
        println("cancelled")
        return
    }
    if activityType == UIActivityTypePostToTwitter {
        println("twitter")
    }
    if activityType == UIActivityTypeMail {
        println("mail")
    }
}

    self.presentViewController(activityViewController, animated: true, completion: nil)
}

这是我的cellForRowAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

var cell:CustomCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as CustomCell

//Assign JSON keys to variables
var rowData: NSDictionary = dataArray[indexPath.row] as NSDictionary
var firstName=rowData["needFirstname"] as String
var descrip=rowData["needDescription"] as String
var city=rowData["needCity"] as String
var state=rowData["needState"] as String
var country=rowData["needCountry"] as String
var needID=rowData["id"] as String

return cell
}

1 个答案:

答案 0 :(得分:0)

想出来,部分原因是Joshua Sullivan的评论。但是,我还在tags中使用了IBOutlet CustomTVCell

将此添加到我的JSON

    cell.shareButton.tag = indexPath.row

我将此添加到我的shareSheet函数中:

    var cell:CustomCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as CustomCell

    if let sharebutton = sender as? UIButton {
        let rowData: NSDictionary = dataArray[sharebutton.tag] as NSDictionary
        cell.passedID = rowData["id"] as String
    }

这是我的CustomTVCell.swift文件:

    var passedID:String = ""
    @IBOutlet var shareButton:UIButton!