我在我的项目4个文件:斯威夫特分离UITableViewDataSource
- myTableViewController.swift
- myTableViewDataSource.swift
- myCustomCell.swift
- myCustomCell.xib
这里是我的实施:
myTableViewController.swift:
import UIKit
class myTableViewController: UIViewController {
let cellIdentifier: String = "myCell"// set same id in storyboard as well
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
setup()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setup(){
let myDataSource = myTableViewDataSource.init(str: "init!")
tableView.registerNib(UINib(nibName: "myCustomCell", bundle: nil), forCellReuseIdentifier: "myCell")
tableView.dataSource = myDataSource
}
myTableViewDataSource.swift:
import UIKit
class myTableViewDataSource: NSObject, UITableViewDataSource, UITableViewDelegate {
init(str:String) {
print(str)
}
// MARK: UITableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
print("section")
return 3
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("row")
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! myCustomCell
print("where's my cell")
return cell;
}
}
什么,我试图做的是简单地使数据源另一个类和指定它的实例变量设置为我的tableView。
更新
根据@Rajan Maheshwari
在setup()
底部加入了tableview.reloadData()
权, 但仍"where's my cell"
没有印刷的部分/行号......为cellForRowAtIndexPath
从未执行。
我在这里得到了什么样的问题?
在setup函数中,只需在最后一行重新载入表格。可以工作吗? –
@RajanMaheshwari打印结果正确!但我的牢房还没有出现。 – bluenowhere
在myTableViewDataSource.swift 中注册你的单元格tableView.registerNib(nibName:“myCustomCell”,bundle:nil),forCellReuseIdentifier:“myCell”) 并告诉结果。 –