![](/img/trans.png)
[英]How do I activate a segue from a tableView where the datasource is from another swift file?
[英]How do I transfer the datasource for a viewTable from another swift file?
我知道這聽起來可能很愚蠢,我已經嘗試了幾個小時,但不知道怎么做。 在這種情況下,如何將標題加載到tableView?
我知道有一個簡單的方法,您可以在ViewController.swift文件中聲明一個數組,並使委托和datasource = self像這樣:
self.drugsTableView.dataSource = self
self.drugsTableView.delegate = self
但是在這種情況下,我想從另一個swift文件中獲取tableView數據源,這就是我的情況:
在項目瀏覽器中,我具有以下文件:
1. ViewController.swift
2. DrugsLibrary.swift
3. DrugList.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 :
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 :
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)
}
}
這是Dropbox上的源代碼
非常感謝您的提示或幫助。
謝謝!
只需創建另一個文件。
class DataSource: NSObject {
lazy var list ... //your data here
}
extension DataSource: UITableViewDataSource {
//method here
}
extension DataSource: UITableViewDelegate {
//method here
}
並在您的ViewController中:
let dataSource = DataSource()
drugsTableView.dataSource = dataSource
drugsTableView.delegate = dataSource
振作起來,當您使用UITableViewDelegate
時,它將變得棘手:)
由於您是新手,因此我將用更多詳細信息更新我的答案。
lazy var list
dataArray
lazy var list
是您的dataArray
,您可以刪除lazy
部分。 我之所以把它留在那里,是因為我不會立即顯示我的表格,因此我不會在需要它之前對其進行查詢。
在您的情況下,只需使用: 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 ...
}
我這里沒有Xcode,所以請自己找到這些方法:)
如果要使用UITableViewDelegate
,則必須創建一個協議,在這些UITableViewDelegate
的方法內部調用該協議,並將它們委托給您的控制器:)
我終於使它工作了,我使用了您的代碼作為提示,但是我不得不使用其他代碼。 這是代碼:
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.