Intercettare lo swipe su una UITableView in Swift
In questo articolo abbiamo visto come rempire una UITableView da JSON.
Poi abbiamo aggiunto anche un loading dialog, ma non è fondamentale per lo scopo di oggi.
Qui vediamo come intercettare lo swipe su una riga della UITableView, usando sempre Swift come linguaggio.
In pratica dobbiamo fare l'override di editActionsForRowAt.
Se siete partiti dagli articoli indicati, dobbiamo aggiungere questo blocco di codice:
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let current = self.tableArray[indexPath.row]
let deleteTitle = NSLocalizedString("Cancella", comment: "Cancella action")
let deleteAction = UITableViewRowAction(style: .destructive, title: deleteTitle) {
(action, indexPath) in
print(current.title)
}
let modificaTitle = NSLocalizedString("Modifica", comment: "Modifica action")
let modificaAction = UITableViewRowAction(style: .normal, title: modificaTitle) {
(action, indexPath) in
print(current.title)
}
// modificaAction.backgroundColor = .green
modificaAction.backgroundColor = UIColor(red:0.04, green:0.38, blue:0.13, alpha:1.0)
return [modificaAction, deleteAction]
}
Abbiamo aggiunto due azioni, che corrispondono a due pulsanti.
A click sul pulsante non facciamo altro che visualizzare il titolo in console; ovviamente dovete implementare le vostre azioni.
Qui sotto il codice completo:
import UIKit
struct Book {
let title: String
let author: String
public init(title: String, author: String) {
self.title = title
self.author = author
}
}
class ViewController: UITableViewController {
var tableArray = [Book] ()
var loadingDialog = LoadingView()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
getJson()
}
func getJson() {
loadingDialog.showSpinner(onView: self.view)
let url = URL(string: "https://www.mattepuffo.com/api/book/get.php")
let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in
guard let jsonData = url else {
return
}
guard let data = try? Data(contentsOf: jsonData) else { return }
guard let json = try? JSONSerialization.jsonObject(with: data, options: []) else{ return }
if let dictionary = json as? [String: Any] {
guard let jsonArray = dictionary["books"] as? [[String: Any]] else {
return
}
for j in jsonArray {
guard let title = j["title"] as? String else { return }
guard let author = j["author"] as? String else { return }
self.tableArray.append(Book(title: title, author: author))
}
}
DispatchQueue.main.async {
self.tableView.reloadData()
self.loadingDialog.removeSpinner()
}
}
task.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
extension ViewController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableArray.count
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let current = self.tableArray[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
cell.textLabel?.text = current.title
cell.detailTextLabel?.text = "\(current.author)"
return cell
}
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let current = self.tableArray[indexPath.row]
let deleteTitle = NSLocalizedString("Cancella", comment: "Cancella action")
let deleteAction = UITableViewRowAction(style: .destructive, title: deleteTitle) {
(action, indexPath) in
print(current.title)
}
let modificaTitle = NSLocalizedString("Modifica", comment: "Modifica action")
let modificaAction = UITableViewRowAction(style: .normal, title: modificaTitle) {
(action, indexPath) in
print(current.title)
}
// modificaAction.backgroundColor = .green
modificaAction.backgroundColor = UIColor(red:0.04, green:0.38, blue:0.13, alpha:1.0)
return [modificaAction, deleteAction]
}
}
Enjoy!
swift uitableviewcontroller json swipe editactionforrowat
Commentami!