我想在我的每个表格视图单元格上添加一个相机按钮,并使用相机拍照并将该照片存储在 Firebase 存储中。我一直在尝试这样做,但我做不到,有人可以帮我这样做吗?这是我的代码
这是我的视图控制器代码:
import UIKit
import Firebase
import Photos
class SubTaskViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
let db = Firestore.firestore()
var documentRef = ""
var indexpath1 = 1
var indexpath2 = 2
var plantdata1: [PlantData] = []
var Tasks: [SubTasks1] = []
var Tasks_IDs: [Tasks1] = []
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
loadTasksData()
}
@IBAction func addButton(_ sender: Any) {
// Settingup our alert controller
let alertController = UIAlertController(title: "Add SubTask", message: "Please enter the subtask title below", preferredStyle: .alert)
// setup the actions
let addAction = UIAlertAction(title: "Add", style: .default) { _ in
// Grab text field text
guard let name = alertController.textFields?.first?.text else {return }
/*self.documentRef = self.db.collection(K.FStore.collectionName).document(self.plantdata1[self.indexpath1].documentID).collection("Tasks").document(name).documentID
print(self.documentRef)*/
self.db.collection(K.FStore.collectionName).document(self.plantdata1[self.indexpath1].documentID).collection("Tasks").document(self.Tasks_IDs[self.indexpath2].documentID).collection("SubTasks").document(name).setData(["Time": Date().timeIntervalSince1970])
// Reload data in table view
// let indextPath = IndexPath(row: 0, section: 0)
// self.tableView.insertRows(at: [indextPath], with: .automatic)
}
// addAction.isEnabled = false
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
// add the text field
alertController.addTextField { textField in
textField.placeholder = "Enter task name"
textField.addTarget(self, action: #selector(self.handleTextChanged), for: .editingChanged)
}
// add the actions
alertController.addAction(addAction)
alertController.addAction(cancelAction)
// present
present(alertController, animated: true)
}
@objc private func handleTextChanged(_ sender: UITextField) {
// Grab the alert controller and add action
guard let alertController = presentingViewController as? UIAlertController,
let addAction = alertController.actions.first,
let text = sender.text
else {return}
// Enable add action based on if text is empty or contains whitespace
addAction.isEnabled = !text.trimmingCharacters(in: .whitespaces).isEmpty
}
func loadTasksData() {
db.collection(K.FStore.collectionName).document(self.plantdata1[self.indexpath1].documentID).collection("Tasks").document(Tasks_IDs[indexpath2].documentID).collection("SubTasks").addSnapshotListener{
(QuerySnapshot, error) in
self.Tasks = []
if let e = error {
print("There was an issue retrieving data from Firestore \(e)")
} else {
if let snapShotDocuments = QuerySnapshot?.documents {
for doc in snapShotDocuments {
let data = doc.data()
// print("\(data[K.Tasks.Time] ?? "It is default value")")
if true
{
let newTask = SubTasks1(Time: (data[K.Tasks.Time] as? String) ?? "Dummy", documentID: doc.documentID, DownloadURL: "www.google.com")
self.Tasks.append(newTask)
DispatchQueue.main.async {
self.tableView.reloadData()
// print("\(self.taskname)")
}
}
}
}
}
}
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let id = segue.identifier
if (id == "SubTaskImageSegue") {
let newVC = segue.destination as! SubTaskImageViewController
let indexpath:NSIndexPath = self.tableView.indexPathForSelectedRow! as NSIndexPath
newVC.indexpath1 = indexpath1
newVC.indexpath2 = indexpath2
newVC.indexpath3 = indexpath.row
newVC.plantdata1 = plantdata1
newVC.Tasks_IDs = Tasks_IDs
newVC.SubTasks_IDs = Tasks
}
}
}
extension SubTaskViewController: UITableViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Tasks.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let newtask = Tasks[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell
cell.textLabel?.text = newtask.documentID
cell.button.tag = indexPath.row
cell.button.addTarget(self, action: #selector(cellButtonTapped(sender:)), for: .touchUpInside)
return cell
}
@objc func cellButtonTapped(sender: UIButton){
print(sender.tag)
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// let postID = plantdata[indexPath.row]
db.collection(K.FStore.collectionName).document(plantdata1[indexpath1].documentID).collection("Tasks").document(Tasks_IDs[indexpath2].documentID).collection("SubTasks").document(Tasks[indexPath.row].documentID).delete { (error) in
if let e = error {
print("Error removing document: \(e)")
} else {
print("Document successfully removed!")
}
}
self.Tasks.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
print("\(indexPath.row)")
tableView.reloadData()
}
}
}
我的表格视图单元格代码:
import UIKit
import Photos
class MyTableViewCell: UITableViewCell {
@IBOutlet weak var button: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func buttonTapped(_ sender: Any) {
}
}