UITableView: Unterschied zwischen den Versionen
(→Links) |
|||
| Zeile 75: | Zeile 75: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| + | |||
| + | === Swipe Actions === | ||
| + | ==== How can I implement a delete gesture to a table view? ==== | ||
| + | |||
| + | You can implement a delete gesture for a table view in iOS using the UITableViewDelegate protocol and the editActionsForRowAt method. | ||
| + | |||
| + | First, you will need to set your table view's delegate to your view controller and conform to the UITableViewDelegate protocol: | ||
| + | |||
| + | <syntaxhighlight lang="swift"> | ||
| + | class MyViewController: UIViewController, UITableViewDelegate { | ||
| + | ... | ||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | Then, in the implementation of your view controller, you can implement the editActionsForRowAt method to return an array of UITableViewRowAction objects that will be displayed when the user swipes on a table view cell. Here is an example that shows a "Delete" action that will delete the selected row when tapped: | ||
| + | |||
| + | <syntaxhighlight lang="swift"> | ||
| + | func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { | ||
| + | let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in | ||
| + | // Delete the row from the data source | ||
| + | self.songs.remove(at: indexPath.row) | ||
| + | tableView.deleteRows(at: [indexPath], with: .fade) | ||
| + | } | ||
| + | |||
| + | return [delete] | ||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | You can also customize the title, background color and other properties of the UITableViewRowAction | ||
| + | The closure passed to the UITableViewRowAction constructor will be called when the user taps on the delete action. In this example it removes the data from the data source and call deleteRows to remove the corresponding row from the table view. | ||
| + | |||
| + | Finally, you also need to set the table view's isEditing property to true. It can be done in the viewDidLoad of the view controller: | ||
| + | |||
| + | <syntaxhighlight lang="swift"> | ||
| + | override func viewDidLoad() { | ||
| + | super.viewDidLoad() | ||
| + | tableView.isEditing = true | ||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | That's it, now when you swipe on a cell in your table view, the "Delete" action will be displayed, and when tapped, the corresponding row will be deleted from the table view. | ||
| + | It is important to note that this is just an example and your implementation may vary depending on your specific use case and the data model you are using. | ||
Version vom 11. Januar 2023, 19:08 Uhr
Links
UIKit Framework
Quickstart
- Öffne dein Storyboard oder deine XIB-Datei und ziehe ein UITableView-Element aus der Bibliothek auf dein View.
- Ziehe ein UITableCell-Element in das UITableView-Element
- Vergebe einen Identifier für das Cell-Element z.B. ReusableCell
- Erstelle ein Outlet für den das UITableView-Element
- Implementiere das UITableViewDataSource-Protokoll in deiner Klasse. Hierdurch wird der tableView wenn er geladen wird einen Datenrequest ausführen. Eine Extension zum Implementieren sieht etwa so aus:
extension MyViewController: UITableViewDataSource{}
- In der viewDidLoad Funktion delegiere die DataSource des TableViews auf den Controller
tableView.dataSource = self
Beispiele
Basic UITableView
Siehe auch Quickstart
import UIKit
struct Song {
let title: String
let frequency: Float
}
import UIKit
class EditViewController: UIViewController {
// Model-Objekte
var frequency: Double? // will receive actual frequency from MetronomeViewController
//var songs = [Song]()
var songs: [Song] = [
Song(title: "Arm aber reich", frequency: 124),
Song(title: "Rebell", frequency: 82.5),
Song(title: "Herz hat recht", frequency: 98)
]
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self // catch datasource delegate
tableView.delegate = self // catch view delegate
}
}
/**
Implement UITableViewDataSource Protocol which is responsible for telling tableview how many cells needed and how to fill them
*/
extension EditViewController: UITableViewDataSource{
// How many rows we need?
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return songs.count
}
// Called for every dataitem -> let's create a cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SongCell", for: indexPath)
let strSongNumber = String(format:"%d / %d",(indexPath.row + 1), (indexPath.count + 1) )
cell.textLabel?.text = "\(strSongNumber) \(songs[indexPath.row].title)"
let strFrequency = String(format:"%.1f bpm",songs[indexPath.row].frequency)
cell.detailTextLabel?.text = strFrequency
return cell
}
}
/**
Implement UITableViewDelegate
Methods for managing selections, configuring section headers and footers, deleting and reordering cells, and performing other actions in a table view.
*/
extension EditViewController: UITableViewDelegate{
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("row: \(indexPath.row) ")
}
}
Swipe Actions
How can I implement a delete gesture to a table view?
You can implement a delete gesture for a table view in iOS using the UITableViewDelegate protocol and the editActionsForRowAt method.
First, you will need to set your table view's delegate to your view controller and conform to the UITableViewDelegate protocol:
class MyViewController: UIViewController, UITableViewDelegate {
...
}
Then, in the implementation of your view controller, you can implement the editActionsForRowAt method to return an array of UITableViewRowAction objects that will be displayed when the user swipes on a table view cell. Here is an example that shows a "Delete" action that will delete the selected row when tapped:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
// Delete the row from the data source
self.songs.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}
return [delete]
}
You can also customize the title, background color and other properties of the UITableViewRowAction The closure passed to the UITableViewRowAction constructor will be called when the user taps on the delete action. In this example it removes the data from the data source and call deleteRows to remove the corresponding row from the table view.
Finally, you also need to set the table view's isEditing property to true. It can be done in the viewDidLoad of the view controller:
override func viewDidLoad() {
super.viewDidLoad()
tableView.isEditing = true
}
That's it, now when you swipe on a cell in your table view, the "Delete" action will be displayed, and when tapped, the corresponding row will be deleted from the table view. It is important to note that this is just an example and your implementation may vary depending on your specific use case and the data model you are using.