[英]Swift TableView for iOS
我已经提到了这个问题 ,并试图实现各种答案。 我正在做一些愚蠢的事情,因为未调用cellForRow,而我只是得到了一个空白的tableView。 还有其他必须重写的数据源或委托方法吗?
谢谢
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//tableView.delegate = self
//tableView.dataSource = self
tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "Cell")
//tableView.registerClass(MyTableViewCell.self, forCellReuseIdentifier: "Cell")
tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
println("cellForRowAtIndexPath")
println()
//variable type is inferred
var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell
if !cell {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
}
cell!.textLabel.text = "Text Label"
cell!.detailTextLabel.text = "Detail Text Label"
return cell
}
}
更新-以下内容似乎可以使用。.感谢您的帮助!
class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//tableView.delegate = self
//tableView.dataSource = self
tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "Cell")
//tableView.registerClass(MyTableViewCell.self, forCellReuseIdentifier: "Cell")
tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
println("cellForRowAtIndexPath")
println()
//variable type is inferred
var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell
if !cell {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
}
cell!.textLabel.text = "Text Label"
cell!.detailTextLabel.text = "Detail Text Label"
return cell
}
}
这是工作代码。 无需在-viewDidLoad()
注册单元格。 UITableView
也作为情节提要中的子视图添加。
如果有人有这种要求。
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as UITableViewCell?
if (cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
}
cell?.textLabel?.text = NSString(format: "%d", indexPath.row) as String
return cell!
}
}
您需要连接TableView
Delegate DataSource
,两者都与tableview
连接,然后只能调用NumberOfRowsInSection
和CellForRowAtIndexpath
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.