3 回答
TA貢獻1906條經驗 獲得超10個贊
以下示例是對We?Swift 的較長帖子的改編和簡化。這就是它的樣子:
創建一個新項目
它可以只是通常的單視圖應用程序。
添加代碼
用以下內容替換ViewController.swift代碼:
import UIKitclass ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
// Data model: These strings will be the data for the table view cells let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat"]
// cell reuse id (cells that scroll out of view can be reused) let cellReuseIdentifier = "cell"
// don't forget to hook this up from the storyboard @IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Register the table view cell class and its reuse id self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
// (optional) include this line if you want to remove the extra empty cell divider lines // self.tableView.tableFooterView = UIView()
// This view controller itself will provide the delegate methods and row data for the table view. tableView.delegate = self
tableView.dataSource = self
}
// number of rows in table view func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.animals.count }
// create a cell for each table view row func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// create a new cell if needed or reuse an old one let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
// set the text from the data model cell.textLabel?.text = self.animals[indexPath.row]
return cell }
// method to run when table view cell is tapped func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You tapped cell number \(indexPath.row).")
}}閱讀代碼內注釋以了解正在發生的事情。亮點是
視圖控制器采用
UITableViewDelegate和UITableViewDataSource協議。該
numberOfRowsInSection方法確定表視圖中將有多少行。該
cellForRowAtIndexPath方法設置每一行。didSelectRowAtIndexPath每次輕敲一行時都會調用該方法。
將表視圖添加到故事板
將a UITableView拖到View Controller上。使用自動布局固定四個邊。
連接奧特萊斯
Control從IB中的表視圖拖動到tableView代碼中的插座。
成品
就這樣。您應該可以立即運行您的應用程序。
這個答案是用Xcode 9和Swift 4測試的
變化
行刪除
如果要允許用戶刪除行,則只需向上面的基本項目添加單個方法。請參閱此基本示例以了解具體方法。
行間距
如果您希望行之間有間距,請參閱此補充示例。
定制單元格
表視圖單元格的默認布局可能不是您所需要的。查看此示例以幫助您開始制作自己的自定義單元格。
動態細胞高度
有時您不希望每個單元格都具有相同的高度。從iOS 8開始,可以根據單元格內容自動設置高度。有關開始使用所需的一切,請參閱此示例。
進一步閱讀
TA貢獻1875條經驗 獲得超3個贊
為了完整起見,以及那些不希望使用Interface Builder的人,這里有一種方法可以完全以編程方式創建與Suragch的答案相同的表- 盡管具有不同的大小和位置。
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView: UITableView = UITableView()
let animals = ["Horse", "Cow", "Camel", "Sheep", "Goat"]
let cellReuseIdentifier = "cell"
override func viewDidLoad() {
super.viewDidLoad()
tableView.frame = CGRectMake(0, 50, 320, 200)
tableView.delegate = self
tableView.dataSource = self
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
self.view.addSubview(tableView)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return animals.count }
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellReuseIdentifier) as UITableViewCell!
cell.textLabel?.text = animals[indexPath.row]
return cell }
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("You tapped cell number \(indexPath.row).")
}}確保你記得import UIKit。
- 3 回答
- 0 關注
- 753 瀏覽
添加回答
舉報
