100天Swift学习挑战#8:滑动删除, SF Symbols系统自带图标库

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (action, sourceView, completionHandler) in
        self.restaurantNames.remove(at: indexPath.row)
        self.restaurantTypes.remove(at: indexPath.row)
        self.restaurantLocations.remove(at: indexPath.row)
        self.restaurantImages.remove(at: indexPath.row)
        self.tableView.deleteRows(at: [indexPath], with: .fade)
        completionHandler(true)
    }
    let shareAction = UIContextualAction(style: .normal, title: "Share") { (action, sourceView, completionHandler) in
        let defaultText = "Just checking in at " + self.restaurantNames[indexPath.row]
        let activityController: UIActivityViewController
        
        if let imageToShare = UIImage(named: self.restaurantImages[indexPath.row]) {
            activityController = UIActivityViewController(activityItems: [defaultText, imageToShare], applicationActivities: nil)
        } else {
            activityController = UIActivityViewController(activityItems: [defaultText], applicationActivities: nil)
        }

        self.present(activityController, animated: true, completion: nil)
        completionHandler(true)
    }
    
    deleteAction.backgroundColor = UIColor(red: 231.0/255.0, green: 76.0/255.0, blue: 60.0/255.0, alpha: 1.0)
    deleteAction.image = UIImage(systemName: "trash")

    shareAction.backgroundColor = UIColor(red: 254.0/255.0, green: 149.0/255.0, blue: 38.0/255.0, alpha: 1.0)
    shareAction.image = UIImage(systemName: "square.and.arrow.up")

    let swipeConfiguration = UISwipeActionsConfiguration(actions: [deleteAction, shareAction])

    return swipeConfiguration
}

上面的UIImage(systemName: "square.and.arrow.up")是调用系统化自带图标,图标查询可以下载:https://developer.apple.com/design/downloads/SF-Symbols.dmg

文章原始链接:https://sijie.wang/posts/swift-challenge-8/

本站文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请保留原始链接