简体   繁体   English

如何从另一个Swift文件传输ViewTable的数据源?

[英]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.

相关问题 如何从数据源来自另一个swift文件的tableView激活序列? - How do I activate a segue from a tableView where the datasource is from another swift file? 如何将数据从应用程序委托传输到另一个类 - How do I transfer data from the app delegate to another class 如何在swift文件中获取类的引用,并在另一个swift文件中进行编码? -xcode /迅速 - How do i get a reference of a class in a swift file and code that in another swift file? - xcode / swift Swift-如何从一个视图控制器转移到另一个? - Swift - how to transfer from one view controller to another? 如何从Swift中的另一个视图调用contentOffset? - How do I call an contentOffset from another view in Swift? 如何从另一个 ViewController (Swift) 访问 UITextField 中的文本? - How do I access text in UITextField from another ViewController (Swift)? 如何将值从 SceneDelegate 文件传输到 ViewController (Swift 5) - How to transfer a value from a SceneDelegate file to a ViewController (Swift 5) 如何从 Swift 文件管理器播放音频文件? - How do I play an audio file from Swift File Manager? 如何将数据从一个表格视图单元格传输到另一个? - How do I transfer data from one table view cell to another? 如何在Swift中从另一个类调用方法? - How do I call a method from another class in Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM