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 关注
- 818 浏览
添加回答
举报
