[英]How do I transfer the datasource for a viewTable from another swift file?
I know this may sound very stupid, i've been trying for hours but couldn't figure out how. 我知道这听起来可能很愚蠢,我已经尝试了几个小时,但不知道怎么做。 How do I load the titles to the tableView in this scenario? 在这种情况下,如何将标题加载到tableView?
I know there is an easy method where you can declare an array in the ViewController.swift file and make the delegate and datasource = self like this: 我知道有一个简单的方法,您可以在ViewController.swift文件中声明一个数组,并使委托和datasource = self像这样:
self.drugsTableView.dataSource = self
self.drugsTableView.delegate = self
But in this case i would like to get the tableView datasource from another swift file, here is how my scenario is going: 但是在这种情况下,我想从另一个swift文件中获取tableView数据源,这就是我的情况:
In the project navigator i have the following files: 在项目浏览器中,我具有以下文件:
1. ViewController.swift 1. ViewController.swift
2. DrugsLibrary.swift 2. DrugsLibrary.swift
3. DrugList.swift 3. DrugList.swift
ViewController.swift ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var drugsTableView: UITableView!
var drugList: DrugsLibraryStruct?
override func viewDidLoad() {
super.viewDidLoad()
self.drugsTableView.dataSource = self
self.drugsTableView.delegate = self
self.navigationItem.title = "Titles"
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return DrugsLibraryStruct().drugsLibraryDictionary.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:"test")
return cell
}
DrugsLibrary.swift : DrugsLibrary.swift :
import Foundation
struct DrugsLibraryStruct {
let drugsLibraryDictionary = [
[
"name": "Drug A",
"subtitle": "Drug A subtitle!",
"icon": "drugA.pdf"
],
[
"name": "Drug B",
"subtitle": "Drug B subtitle",
"icon": "drugb.pdf"
]
]
} }
DrugList.swift : DrugList.swift :
import Foundation
import UIKit
struct DrugListStruct {
var name: String?
var subtitle: String?
var icon: UIImage?
init(index: Int) {
let drugsLibrary = DrugsLibraryStruct().drugsLibraryDictionary
let drugListDictionary = drugsLibrary[index]
name = drugListDictionary["name"] as String!
subtitle = drugListDictionary["subtitle"] as String!
let iconName = drugListDictionary["icon"] as String!
icon = UIImage(named: iconName)
}
} }
Here is the source code on Dropbox 这是Dropbox上的源代码
I would really appreciate your hints or help. 非常感谢您的提示或帮助。
Thank you! 谢谢!
Just create another file. 只需创建另一个文件。
class DataSource: NSObject {
lazy var list ... //your data here
}
extension DataSource: UITableViewDataSource {
//method here
}
extension DataSource: UITableViewDelegate {
//method here
}
and in your ViewController: 并在您的ViewController中:
let dataSource = DataSource()
drugsTableView.dataSource = dataSource
drugsTableView.delegate = dataSource
Brace yourself, it will get tricky when you use UITableViewDelegate
:) 振作起来,当您使用UITableViewDelegate
时,它将变得棘手:)
Since you're new, I update my answer with more details. 由于您是新手,因此我将用更多详细信息更新我的答案。
lazy var list
is your dataArray
, you can remove the lazy
part. lazy var list
dataArray
lazy var list
是您的dataArray
,您可以删除lazy
部分。 I leave it there because I don't display my table immediately, so I don't query it until it's needed. 我之所以把它留在那里,是因为我不会立即显示我的表格,因此我不会在需要它之前对其进行查询。
In your case, just use: var myArray = ...
在您的情况下,只需使用: var myArray = ...
extension DataSource: UITableViewDataSource {
func tableView - numberOfRowsInsection (your method above)
func tableView - cellForRowAtIndexPath (your method above)
//these methods are required, since they're needed for display data on table
}
extension DataSource: UITableViewDelegate {
//methods which will be called when you interact with the table
func tableView - didSelectRowAtIndexPath ...
}
I don't have Xcode here, so find these methods yourself :) 我这里没有Xcode,所以请自己找到这些方法:)
If you want to use the UITableViewDelegate
, you have to create a protocol, which is called inside these UITableViewDelegate
's methods and delegate them back to your controller :) 如果要使用UITableViewDelegate
,则必须创建一个协议,在这些UITableViewDelegate
的方法内部调用该协议,并将它们委托给您的控制器:)
I finally had it working, I used your code as a hint but I had to use a different code. 我终于使它工作了,我使用了您的代码作为提示,但是我不得不使用其他代码。 Here's the code: 这是代码:
class Datasource: NSData, UITableViewDataSource, UITableViewDelegate {
let itemsArray = ["Item 1","Item 2","Item 3","Item 4"]
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemsArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:"test")
cell.textLabel!.text = self.itemsArray[indexPath.row]
return cell
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.