接续上个礼拜的文章:范例表格程式
首先点开 Asset.xcassets 加入一张图(随机都行)将图片名称改为 restaurant
回到程式码,加入以下程式码第 13 行
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]
cell.imageView?.image = UIImage(named: "restaurant")
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}这样就可以把餐厅图片(假装那个人头是餐厅)秀出来
加入以下程式码可以隐藏状态栏(第 18-20 行)
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]
cell.imageView?.image = UIImage(named: "restaurant")
return cell
}
override var prefersStatusBarHidden: Bool{
return true
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}这样就可以把状态栏隐藏
接下来将三张 user 的图片放进来,并且改名成 restaurant1~3.素材参考:堆叠视图
将程式码改成以下模样(新增第 5 行,修改第 14 行)
import UIKit
class ViewController: UIViewController, UITableViewDataSource {
var restarantNames = ["Cafe Deaned", "Homei", "Teakha"]
var restaurantImages = ["restaurant1", "restaurant2", "restaurant3"]
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]
cell.imageView?.image = UIImage(named: restaurantImages[indexPath.row])
return cell
}
override var prefersStatusBarHidden: Bool{
return true
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}执行模拟器,三张不同的餐厅(人?)可以正常显示
Latest posts by SHXJ (see all)
- 受保护的内容: NAS 版 Mathbot 管理网站与 Linebot 启动方法 - 2024 年 11 月 15 日
- Realtime 啥鬼的 - 2021 年 6 月 15 日
- nodejs 数学游戏 - 2021 年 6 月 8 日




