分离 ViewController 和 UITableViewDataSource
Separating ViewController and UITableViewDataSource
我试图通过将 UITableViewDataSource(和 Delegate)移动到一个单独的 class 来清理拥挤的 ViewController。
虽然数据源在 ViewController 中(见下文),但它工作正常
class ViewController: UIViewController, UITableViewDataSource{
//MARK: Properties
@IBOutlet weak var myTableView: UITableView!
let cellIdentifier = "myTableViewCell"
let myArray = ["Label one", "Label two", "Label three"]
override func viewDidLoad()
{
super.viewDidLoad()
myTableView.dataSource = self
}
override func didReceiveMemoryWarning()
{super.didReceiveMemoryWarning()}
//MARK: TableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{return 1}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{return myArray.count}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! myTableViewCell
cell.myLabel.text = myArray[indexPath.row]
return cell
}
}
用myTableViewCell class
class myTableViewCell: UITableViewCell{
@IBOutlet weak var myLabel: UILabel!
override func awakeFromNib()
{super.awakeFromNib()}
override func setSelected(selected: Bool, animated: Bool)
{super.setSelected(selected, animated: animated)}
}
这工作正常,并使用从 myArray 中的字符串填充的标签填充基本 table。
然而,当我将 DataSource 移动到它自己的 class 时,如下所示,它不起作用
ViewController
class ViewController: UIViewController
{
//MARK: Properties
@IBOutlet weak var myTableView: UITableView!
override func viewDidLoad()
{
super.viewDidLoad()
myTableView.dataSource = myDataSource()
}
override func didReceiveMemoryWarning()
{super.didReceiveMemoryWarning()}
}
这是 myDataSource class
class myDataSource: NSObject, UITableViewDataSource
{
let cellIdentifier = "myTableViewCell"
let myArray = ["Label one", "Label two", "Label three"]
//MARK: TableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{return 1}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{return myArray.count}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! myTableViewCell
cell.myLabel.text = myArray[indexPath.row]
return cell
}
}
(myTableViewCell 保持不变)
此设置仅输出一个空 table,尽管我所做的只是将代码从 ViewController 复制粘贴到 myDataSource。我错过了什么? (除了代表之外。我稍后会处理,首先我需要找到数据源的问题)。
我是 swift 的菜鸟,所以我很难理解我哪里出错了。任何帮助都将不胜感激。如果您能在回答中记住我刚刚开始,那么请尽量不要在没有解释的情况下向我抛出太多复杂的概念。谢谢
将单独的 class 声明为 ViewController
class
的扩展
extension ViewController: UITableViewDataSource
{
let cellIdentifier = "myTableViewCell"
let myArray = ["Label one", "Label two", "Label three"]
//MARK: TableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{return 1}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{return myArray.count}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! myTableViewCell
cell.myLabel.text = myArray[indexPath.row]
return cell
}
}
我也总是在扩展声明中使用委托方法,但在与主文件相同的文件中 class
可能您的数据源中的代码永远不会被调用,因为您的数据源没有被 table 这里 myTableView.dataSource = myDataSource()
保留,所以基本上会立即释放。要解决此问题,请将数据源保留为 属性.
我试图通过将 UITableViewDataSource(和 Delegate)移动到一个单独的 class 来清理拥挤的 ViewController。
虽然数据源在 ViewController 中(见下文),但它工作正常
class ViewController: UIViewController, UITableViewDataSource{
//MARK: Properties
@IBOutlet weak var myTableView: UITableView!
let cellIdentifier = "myTableViewCell"
let myArray = ["Label one", "Label two", "Label three"]
override func viewDidLoad()
{
super.viewDidLoad()
myTableView.dataSource = self
}
override func didReceiveMemoryWarning()
{super.didReceiveMemoryWarning()}
//MARK: TableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{return 1}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{return myArray.count}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! myTableViewCell
cell.myLabel.text = myArray[indexPath.row]
return cell
}
}
用myTableViewCell class
class myTableViewCell: UITableViewCell{
@IBOutlet weak var myLabel: UILabel!
override func awakeFromNib()
{super.awakeFromNib()}
override func setSelected(selected: Bool, animated: Bool)
{super.setSelected(selected, animated: animated)}
}
这工作正常,并使用从 myArray 中的字符串填充的标签填充基本 table。
然而,当我将 DataSource 移动到它自己的 class 时,如下所示,它不起作用
ViewController
class ViewController: UIViewController
{
//MARK: Properties
@IBOutlet weak var myTableView: UITableView!
override func viewDidLoad()
{
super.viewDidLoad()
myTableView.dataSource = myDataSource()
}
override func didReceiveMemoryWarning()
{super.didReceiveMemoryWarning()}
}
这是 myDataSource class
class myDataSource: NSObject, UITableViewDataSource
{
let cellIdentifier = "myTableViewCell"
let myArray = ["Label one", "Label two", "Label three"]
//MARK: TableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{return 1}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{return myArray.count}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! myTableViewCell
cell.myLabel.text = myArray[indexPath.row]
return cell
}
}
(myTableViewCell 保持不变)
此设置仅输出一个空 table,尽管我所做的只是将代码从 ViewController 复制粘贴到 myDataSource。我错过了什么? (除了代表之外。我稍后会处理,首先我需要找到数据源的问题)。 我是 swift 的菜鸟,所以我很难理解我哪里出错了。任何帮助都将不胜感激。如果您能在回答中记住我刚刚开始,那么请尽量不要在没有解释的情况下向我抛出太多复杂的概念。谢谢
将单独的 class 声明为 ViewController
class
extension ViewController: UITableViewDataSource
{
let cellIdentifier = "myTableViewCell"
let myArray = ["Label one", "Label two", "Label three"]
//MARK: TableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{return 1}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{return myArray.count}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! myTableViewCell
cell.myLabel.text = myArray[indexPath.row]
return cell
}
}
我也总是在扩展声明中使用委托方法,但在与主文件相同的文件中 class
可能您的数据源中的代码永远不会被调用,因为您的数据源没有被 table 这里 myTableView.dataSource = myDataSource()
保留,所以基本上会立即释放。要解决此问题,请将数据源保留为 属性.