UITableView: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
Zeile 20: Zeile 20:
 
     let frequency: Float
 
     let frequency: Float
 
}
 
}
 +
 +
import UIKit
  
 
class EditViewController: UIViewController {
 
class EditViewController: UIViewController {
Zeile 25: Zeile 27:
 
     // Model-Objekte
 
     // Model-Objekte
 
     var frequency: Double? // will receive actual frequency from MetronomeViewController
 
     var frequency: Double? // will receive actual frequency from MetronomeViewController
−
 
 
     //var songs = [Song]()
 
     //var songs = [Song]()
 
     var songs: [Song] = [
 
     var songs: [Song] = [
Zeile 37: Zeile 38:
 
     override func viewDidLoad() {
 
     override func viewDidLoad() {
 
         super.viewDidLoad()
 
         super.viewDidLoad()
−
         tableView.dataSource = self
+
         tableView.dataSource = self // catch datasource delegate
 +
        tableView.delegate = self // catch view delegate
 
     }
 
     }
 
}
 
}
  
 
/**
 
/**
−
  implement UITableViewDataSource Protocol
+
  Implement UITableViewDataSource Protocol which is responsible for telling tableview how many cells needed and how to fill them
−
This is responsible for telling tableview how many cells needed and how to fill them
+
*/
−
*/
 
 
extension EditViewController: UITableViewDataSource{
 
extension EditViewController: UITableViewDataSource{
−
 
 
     // How many rows we need?
 
     // How many rows we need?
 
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
 
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
 
         return songs.count
 
         return songs.count
 
     }
 
     }
−
 
 
     // Called for every dataitem -> let's create a cell
 
     // Called for every dataitem -> let's create a cell
 
     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 
     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
Zeile 63: Zeile 62:
 
}
 
}
  
−
</syntaxhighlight>
+
/**
 +
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) ")
 +
    }
 +
}

Version vom 8. Januar 2023, 15:04 Uhr

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 <syntaxhighlight lang="switch"> 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) ")
   }

}