Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. import UIKit
  2.  
  3. class ViewController: UIViewController {
  4.  
  5. @IBOutlet weak var tableView: UITableView!
  6.  
  7. override func viewDidLoad() {
  8. super.viewDidLoad()
  9. tableView.dataSource = self
  10. tableView.delegate = self
  11. }
  12.  
  13. }
  14.  
  15. extension ViewController: UITableViewDataSource {
  16. ///總共有幾個Row
  17. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  18. return 10
  19. }
  20.  
  21. ///每個Row所顯示的Cell
  22. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  23. ///透過Identifier來取得Cell
  24. guard let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell") else {
  25. fatalError()
  26. }
  27.  
  28. cell.textLabel?.text = "\(indexPath.row)"
  29. return cell
  30. }
  31. }
  32.  
  33. extension ViewController: UITableViewDelegate {
  34.  
  35. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  36. print("使用者點選了 \(indexPath.row)")
  37. }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement