代码之家  ›  专栏  ›  技术社区  ›  Turnipdabeets

UITableViewRowAction与UISwipeActionsConfiguration

  •  0
  • Turnipdabeets  · 技术社区  · 7 年前

    这两个API似乎得到了相同的结果。在哪种情况下,使用一个比另一个更好?

    override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
      let deleteAction = UITableViewRowAction(style: .destructive,
                                              title: "Delete") { (action, indexPath) in
        print("DELETE")
      }
      return [deleteAction]
    }
    

    override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
      let DeleteAction = UIContextualAction(style: .destructive, title: "Delete", handler: { (action, view, success) in
          print("Delete")
      })
      DeleteAction.backgroundColor = .red
      return UISwipeActionsConfiguration(actions: [DeleteAction])
    }
    
    1 回复  |  直到 7 年前
        1
  •  9
  •   Zouhair Sassi minedun6    7 年前

    它的功能基本相同,但自iOS 11发布以来,就可以使用刷卡操作,并且具有一些新功能:

    • 您可以设置尾随滑动和前导滑动的操作
    • 您可以设置动作的图像 action.image = UIImage(...) . 如果有足够的空间,它会显示图像和标题 enter image description here

    UITableViewRowActions 将如何弃用 UITableView

    tableView:trailingSwipeActionsConfigurationForRowAtIndexPath: 而不是此方法,该方法将在将来的版本中被弃用。

        2
  •  1
  •   Guri S    6 年前

    这是为我工作的代码

    internal func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    
       // let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
        let contextItem = UIContextualAction(style: .destructive, title: "") {  (contextualAction, view, boolValue) in
        // delete item at indexPath
           //if self.isfilterenabled == true {
             //   return
            //}
    
    
            if self.isfilterenabled == true {
                //entryFilter.filteredEntries[indexPath.row]
                self.entryFilter.filteredEntries.remove(at: indexPath.row)
            } else {
                self.data.remove(at: indexPath.row)
            }
    
            self.table.deleteRows(at: [indexPath], with: .fade)
            self.save()
        }
    
    
    
        //let share = UITableViewRowAction(style: .normal, title: "SavePDF") { (action, indexPath) in
            // share item at indexPath
        let contextItemSave = UIContextualAction(style: .normal, title: "") {  (contextualAction, view, boolValue) in
    
    
    
    
    
            let alert  = UIAlertController(title: "Done! ", message: "PDF has been saved " ,preferredStyle: .alert)
    
            let okAction = UIAlertAction(title: "OK ", style: .default, handler: nil)
            alert.addAction(okAction)
    
            alert.popoverPresentationController?.sourceView = self.view // so that iPads won't crash
    
            // exclude some activity types from the list (optional)
            //activityViewController.excludedActivityTypes = [ UIActivityTypeAirDrop, UIActivityTypePostToFacebook ]
    
            // present the view controller
            self.present(alert, animated: true, completion: nil)
            // present(alert, animated : true, completion : nil )
    
        }
    
       // share.backgroundColor = UIColor.blue
    
        contextItemSave.image = UIImage(named:"PDF.jpg")
        contextItem.image = UIImage(named:"delete.jpg")
        let swipeActions = UISwipeActionsConfiguration(actions: [contextItem,contextItemSave])
    
        return swipeActions
        //return [delete, share]
    }`
    
        3
  •  0
  •   ysnzlcn    5 年前

    我已经为创建了一个扩展 UISwipeActionsConfiguration UIContextualAction 的 image 财产。

    extension UISwipeActionsConfiguration {
    
        public static func makeTitledImage(
            image: UIImage?,
            title: String,
            textColor: UIColor = .white,
            font: UIFont = .systemFont(ofSize: 14),
            size: CGSize = .init(width: 50, height: 50)
        ) -> UIImage? {
            
            /// Create attributed string attachment with image
            let attachment = NSTextAttachment()
            attachment.image = image
            let imageString = NSAttributedString(attachment: attachment)
            
            /// Create attributed string with title
            let text = NSAttributedString(
                string: "\n\(title)",
                attributes: [
                    .foregroundColor: textColor,
                    .font: font
                ]
            )
            
            /// Merge two attributed strings
            let mergedText = NSMutableAttributedString()
            mergedText.append(imageString)
            mergedText.append(text)
            
            /// Create label and append that merged attributed string
            let label = UILabel(frame: CGRect(x: 0, y: 0, width: size.width, height: size.height))
            label.textAlignment = .center
            label.numberOfLines = 2
            label.attributedText = mergedText
            
            /// Create image from that label
            let renderer = UIGraphicsImageRenderer(bounds: label.bounds)
            let image = renderer.image { rendererContext in
                label.layer.render(in: rendererContext.cgContext)
            }
            
            /// Convert it to UIImage and return
            if let cgImage = image.cgImage {
                return UIImage(cgImage: cgImage, scale: UIScreen.main.scale, orientation: .up)
            }
            
            return nil
        }
    }
    

    public func tableView(
            _ tableView: UITableView,
            trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath
        ) -> UISwipeActionsConfiguration? 
    {
            let deleteAction = UIContextualAction(
                style: .normal,
                title:  nil,
                handler: { [weak self] (_, _, success: (Bool) -> Void) in
                    success(true)
                    print("Your action in here")
                }
            )
            
            deleteAction.image = UISwipeActionsConfiguration.makeTitledImage(
                image: UIImage(named: "delete_icon"),
                title: "Delete")
            )
            deleteAction.backgroundColor = .orange
            return UISwipeActionsConfiguration(actions: [deleteAction])
    }