iOS 8 Swift - 使用核心数据实现共享表

时间:2015-03-20 16:16:35

标签: ios swift ios8

我试图在我的iOS应用程序中实现共享表,但我收到了错误。

这是我的代码:

class DetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet var backpackerSpotImageView:UIImageView!
    @IBOutlet var tableView:UITableView!


    var backpackerSpot:BackpackerSpot?


    override func viewDidLoad() {
        super.viewDidLoad()

        // customizing background of tableview
        self.tableView.backgroundColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 0.2)

        // remove extra separators
        self.tableView.tableFooterView = UIView(frame: CGRectZero)

        // change the color of the separator
        self.tableView.separatorColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 0.8)

        // self-sizing cells
        tableView.estimatedRowHeight = 36.0
        tableView.rowHeight = UITableViewAutomaticDimension

        // Do any additional setup after loading the view.
        if let spotImage = backpackerSpot?.spotImage
        {
        self.backpackerSpotImageView.image = UIImage(data:spotImage)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }

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


        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as DetailTableViewCell

        // make cell transparent so background color can be seen
        cell.backgroundColor = UIColor.clearColor()

        cell.mapButton.hidden = true

        switch indexPath.row {
        case 0:
            cell.fieldLabel.text = "Name"
            cell.valueLabel.text = backpackerSpot?.spotName
        case 1:
            cell.fieldLabel.text = "Location"
            cell.valueLabel.text = backpackerSpot?.spotLocation
            cell.mapButton.hidden = false
        case 2:
            cell.fieldLabel.text = "Notes"
            cell.valueLabel.text = backpackerSpot?.spotNote
        default:
            cell.fieldLabel.text = ""
            cell.valueLabel.text = ""

        }

        return cell
    }

    @IBAction func shareSheet(sender:AnyObject) {
        let firstActivityItem = backpackerSpot?.spotName
        let secondActivityItem = backpackerSpot?.spotLocation

        let activityViewController = UIActivityViewController(
            activityItems: [firstActivityItem, secondActivityItem], applicationActivities: nil)

        activityViewController.excludedActivityTypes = [
            UIActivityTypePostToVimeo,
            UIActivityTypePostToTencentWeibo,
            UIActivityTypePostToFlickr,
            UIActivityTypePostToWeibo,
            UIActivityTypeSaveToCameraRoll,
            UIActivityTypePrint,
            UIActivityTypeAssignToContact,
            UIActivityTypeAddToReadingList
        ]

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

    }

在我声明activityViewController的行上,我收到以下错误:

Cannot invoke 'init' with an argument list of type '(activityItems: $T2, applicationActivities: NilLiteralConvertible)'

我对开发iOS应用程序相对较新,而我遇到的最大问题之一就是解密错误消息。如果有人能指出我正确的方向,我们非常感激。谢谢。

2 个答案:

答案 0 :(得分:2)

尝试更改这些行:

let firstActivityItem = backpacker!.spotName

let secondActivityItem  = backpacker!.spotLocation

两者一定不能为零。

答案 1 :(得分:2)

好吧我弄清楚出了什么问题。

首先,我没有正确定义activityViewController。其次,我不得不打开backpackerSpot。我还将函数的发件人更改为UIBarButtonItem

以下是代码:

@IBAction func shareSheet(sender:UIBarButtonItem) {
    let firstActivityItem = backpackerSpot!.spotName
    let secondActivityItem = backpackerSpot!.spotLocation

    let activityViewController : UIActivityViewController = UIActivityViewController(
        activityItems: [firstActivityItem, secondActivityItem], applicationActivities: nil)

    activityViewController.excludedActivityTypes = [
        UIActivityTypePostToVimeo,
        UIActivityTypePostToTencentWeibo,
        UIActivityTypePostToFlickr,
        UIActivityTypePostToWeibo,
        UIActivityTypeSaveToCameraRoll,
        UIActivityTypePrint,
        UIActivityTypeAssignToContact,
        UIActivityTypeAddToReadingList
    ]

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

}