我的问题是,如何在 UITableView 中创建动态原型(prototype)单元格。我在互联网上浏览了一下,只找到了 UITableViewControllers 的教程和代码,但没有找到我想在我的应用程序中使用的 UITableViews。因此,如果有人可以帮忙编写一些代码,那就太好了。
谢谢。
最佳答案
这是创建 UITableView
的代码:
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
let tableData = ["1","2","3","4"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return tableData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
cell.textLabel?.text = self.tableData[indexPath.row]
return cell
}
}
关于swift - 快速在 UITableView 中创建单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31864115/