新增一個專案,名稱 SimpleTable
點擊 Main.storyboard,新增一個 Table View
把這個 Table view 填滿整個螢幕
把 Prototype Cells 設定為 1
點一下第一個儲存格
把 style 選擇 Basic
Identifier 設定為 datacell
選取 Table view,點擊下面的 Add New Constraints,並將四條紅線選起來,按下 Add 4 Constrains
點擊 ViewController.swift,將表格的協定加上去
按下 Fix 他就會幫我們把方法加進來

就像這樣
在第 12 行新增餐廳的名稱,並且返回菜單總數(第十四行)
//
// ViewController.swift
// SimpleTable
//
// Created by SHXJ on 2020/10/8.
// Copyright © 2020 SHXJ. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UITableViewDataSource {
var restarantNames = ["Cafe Deaned", "Homei", "Teakha"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return restarantNames.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
找到儲存格物件並且設定顯示內容(第十八行到第二十二行)
//
// ViewController.swift
// SimpleTable
//
// Created by SHXJ on 2020/10/8.
// Copyright © 2020 SHXJ. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UITableViewDataSource {
var restarantNames = ["Cafe Deaned", "Homei", "Teakha"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return restarantNames.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "datacell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
cell.textLabel?.text = restarantNames[indexPath.row]
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
到 Main.storyboard ,對著 TableView 按下滑鼠右鍵
接著按照動畫將兩個東西拉進去
按下模擬器,畫面上有出現所有餐廳的名字就成功惹
Latest posts by SHXJ (see all)
- 受保護的內容: NAS 版 Mathbot 管理網站與 Linebot 啟動方法 - 2024 年 11 月 15 日
- Realtime 啥鬼的 - 2021 年 6 月 15 日
- nodejs 數學遊戲 - 2021 年 6 月 8 日













在〈範例表格程式〉中有 1 則留言