Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class ViewController: UIViewController {
- @IBOutlet weak var tableView: UITableView!
- override func viewDidLoad() {
- super.viewDidLoad()
- tableView.dataSource = self
- tableView.delegate = self
- }
- }
- extension ViewController: UITableViewDataSource {
- ///總共有幾個Row
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return 10
- }
- ///每個Row所顯示的Cell
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- ///透過Identifier來取得Cell
- guard let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell") else {
- fatalError()
- }
- cell.textLabel?.text = "\(indexPath.row)"
- return cell
- }
- }
- extension ViewController: UITableViewDelegate {
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- print("使用者點選了 \(indexPath.row)")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement