【发布时间】:2017-07-10 16:40:47
【问题描述】:
我是 Swift 新手,我一直在开发一个简单的应用程序,它允许我将来自 MVC 的信息呈现到 TableView。我想做的是 - 当在TableView 中选择一行时,它将在新的ViewController 中显示其余信息。我已经设法使用 init 方法将 MVC 中的“名称”变量调用到原始 tableView,但是我正在努力跟踪 tableViewCells 并将剩余数据放在 viewController 中。
代码如下:
// Model Variables
import Foundation
class Close: NSObject {
var name: String
var technique: String
var example: String
var howItWorks: String
init(name: String, technique: String, example: String, howItWorks: String) {
self.name = name
self.technique = technique
self.example = example
self.howItWorks = howItWorks
}
}
import Foundation
class CloseGroup: NSObject {
var name: String
var closesArray: [Close]
init(name: String, closesArray: [Close]) {
self.name = name
self.closesArray = closesArray
}
}
这里是我要在 TableView 和 ViewController 中显示的数据的地方
class CloseDataManager: NSObject {
static func createCloseGroup() -> [CloseGroup] {
let basicCloses = CloseGroup(name: "Basic Closes",
closesArray: [
Close(name: "Ask the manager",
technique: "One thing we can all agree on is that people love to spend money right? Nevertheless we all have a limit, some people have no problem spending a lot while other people may not want to spend that much. This doesnt take away from the fact that regardless whatever your spending you want the best for your money, now once you've been able to create value and the prospect is interest however after looking at the price they say 'As nice as it is, I simply cant afford it' - this technique comes in to play.",
example: "Look I can understand that this is a bit more then you expected to pay, let me ask you how much can/are you afford/willing to pay per month? 300…Ok Perfect, we have a package for that… ",
howItWorks: "This close works by carefully tailoring finance of a product/service to fit the other persons ability or willingness to pay. ‘I cant afford this’most of the time is not an objection, its an excuse. If they really don’t want to purchase the product they will rapidly jump to other objections. Be careful when speaking to clients about finance as this subject to many people is very touchy, be aware of the words that you use because once you cross the line of finance the clients guard will go back up and you will need to put in some work to bring the barrier back down."),
Close(name: "Compliment Close",
technique: "One thing we can all agree on is that people love to spend money right? Nevertheless we all have a limit, some people have no problem spending a lot while other people may not want to spend that much. This doesnt take away from the fact that regardless whatever your spending you want the best for your money, now once you've been able to create value and the prospect is interest however after looking at the price they say 'As nice as it is, I simply cant afford it' - this technique comes in to play.",
example: "Look I can understand that this is a bit more then you expected to pay, let me ask you how much can/are you afford/willing to pay per month? 300…Ok Perfect, we have a package for that… ",
howItWorks: "This close works by carefully tailoring finance of a product/service to fit the other persons ability or willingness to pay. ‘I cant afford this’most of the time is not an objection, its an excuse. If they really don’t want to purchase the product they will rapidly jump to other objections. Be careful when speaking to clients about finance as this subject to many people is very touchy, be aware of the words that you use because once you cross the line of finance the clients guard will go back up and you will need to put in some work to bring the barrier back down.") )]
//这是我的TableView代码:
import UIKit
class BasicClosesTableViewController: UITableViewController {
var basicCloses: CloseGroup
init(basicCloses: CloseGroup) {
self.basicCloses = basicCloses
super.init(style: .plain)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "BasicClosesCell")
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return basicCloses.closesArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BasicClosesCell", for: indexPath)
let close: Close = basicCloses.closesArray[indexPath.row]
cell.textLabel?.text = close.name
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let close: Close = basicCloses.closesArray[indexPath.row]
print(close.example)
}
}
//这是我想从我的 MVC 中展示剩余变量的地方:
import UIKit
class ExplanationTableViewController: UITableViewController {
var allCloses: CloseGroup
init(allCloses: CloseGroup) {
self.allCloses = allCloses
super.init(style: .plain)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
【问题讨论】:
-
你能试试这个必需的初始化吗?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) }
标签: ios swift model-view-controller