【问题标题】:Swift : How can I use delete and share button in Cell?Swift:如何在 Cell 中使用删除和共享按钮?
【发布时间】:2015-06-22 01:30:03
【问题描述】:

我正在使用 commitEditingStyle 进行删除。我在单元格中添加了共享按钮。现在我只是在单元格中使用共享按钮。我想在单元格中使用这两个。我应该使用像我的“分享”按钮这样的自定义删除按钮吗?我可以为此做些什么?

我的代码在这里:

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == UITableViewCellEditingStyle.Delete {
            // Delete the row from the data source

            var object: PFObject =  self.noteObjects.objectAtIndex(indexPath.row) as! PFObject
            object.delete()

            self.fetchAllObjectsFromLocalDatastore()
            self.fetchAllObjects()
            tableView.reloadData()
            //tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        }
    }

    override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]?
    {

        var shareAction = UITableViewRowAction(style: .Normal, title: "Share") { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in

            let object: PFObject = self.noteObjects[indexPath.row] as! PFObject
            let firstActivityItem = object["title"] as! String
            let secondActivityItem = object["text"] as! String

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

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


        }

        shareAction.backgroundColor = UIColor.blueColor()

        return [shareAction]

    }

【问题讨论】:

    标签: ios swift uitableview xcode6


    【解决方案1】:

    editActionsForRowAtIndexPath中返回多个UITableViewRowAction

    override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]?
    {
        var shareAction = UITableViewRowAction(style: .Normal, title: "Share") { 
            (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in
            let object: PFObject = self.noteObjects[indexPath.row] as! PFObject
            let firstActivityItem = object["title"] as! String
            let secondActivityItem = object["text"] as! String
    
            let activityViewController = UIActivityViewController(activityItems: [firstActivityItem, secondActivityItem], applicationActivities: nil)
    
            self.presentViewController(activityViewController, animated: true, completion: nil)
        }
    
        var deleteAction = UITableViewRowAction(style: .Destructive, title: "Delete") {
            (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in
            //...
        }
    
        return [deleteAction, shareAction]
    }
    

    【讨论】:

      【解决方案2】:

      好的,我解决了这个问题。我也将自定义单元格用于删除操作,并且有效。

      这是在单元格中删除和共享操作的最终代码:

      override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
      }
      
      override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]?
      {
      
          var shareAction = UITableViewRowAction(style: .Normal, title: "Share") { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in
      
              let object: PFObject = self.noteObjects[indexPath.row] as! PFObject
              let firstActivityItem = object["title"] as! String
              let secondActivityItem = object["text"] as! String
      
              let activityViewController = UIActivityViewController(activityItems: [firstActivityItem, secondActivityItem], applicationActivities: nil)
      
              self.presentViewController(activityViewController, animated: true, completion: nil)
      
      
          }
      
          var deleteAction = UITableViewRowAction(style: .Normal, title: "Delete") { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in
      
              var object: PFObject =  self.noteObjects.objectAtIndex(indexPath.row) as! PFObject
              object.delete()
      
              self.fetchAllObjectsFromLocalDatastore()
              self.fetchAllObjects()
              tableView.reloadData()
      
          }
      
          shareAction.backgroundColor = UIColor.blueColor()
      
          return [deleteAction,shareAction]
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-14
        • 2017-11-03
        • 1970-01-01
        • 2016-07-17
        • 1970-01-01
        • 2014-02-19
        • 2020-07-21
        相关资源
        最近更新 更多