
UITableViewController{ var items = ["Item 1", "Item 2", "Item 3"]
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = items[indexPath.row]
return cell
}
} 這是基本的 UITableViewController 實(shí)現(xiàn)。它有一個(gè) items 數(shù)組來(lái)存儲(chǔ)表格中的數(shù)據(jù)。在 viewDidLoad 方法中,設(shè)置視圖的背景顏色為白色。在 tableView: numberOfRowsInSection: 方法中,返回 items 數(shù)組的長(zhǎng)度。在 tableView: cellForRowAt: 方法中,創(chuàng)建 UITableViewCell,并將 items 數(shù)組中的元素設(shè)置為單元格的文本。這個(gè)實(shí)現(xiàn)適合簡(jiǎn)單的表格視圖,可以展示一個(gè)簡(jiǎn)單的列表。