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

标签 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 应用程序还比较陌生,我遇到的最大问题之一是破译错误消息。如果有人能指出我正确的方向,我将不胜感激。谢谢。

最佳答案

尝试更改这些行:

let firstActivityItem = backpacker!.spotName

let secondActivityItem  = backpacker!.spotLocation

两者不能为零。

关于iOS 8 Swift - 使用核心数据实现共享表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29171189/

相关文章:

iphone - UIImageView 不遵循 origin.x 位置

swift - 没有 UI 元素的 Segue 和 Unwind segue

ipad - 如何在 iOS 8 上使用 UIModalPresentationFormSheet 调整 View 大小

objective-c - Objective C-iCal 不在 iOS 9 中为其创建自定义日历和新事件

带有附件 View 的 iOS 8 自调整单元格

iOS 属性文本删除/n 换行符

ios - 处理 JSON 响应中的字符串或整数

ios - 将 OneSignal PlayerID 附加到 iOS Webview URL

swift - 为什么 `+=` 不能与隐式解包的选项一起使用

swift - 隐藏后如何在不同的地方重新生成