代碼: 選擇全部
import Foundation
import UIKit
struct Objects
{
var sectionName : String!
var sectionObjects : [String]!
}
class ViewModel
{
var A: String
var B: String
init(a: String, b: String)
{
self.A = a
self.B = b
}
class func Info(model: ViewModel) -> [Objects]
{
var result: [Objects] = [Objects]()
result.append(Objects(sectionName: "My A", sectionObjects: [model.A]))
result.append(Objects(sectionName: "My B", sectionObjects: [model.B]))
return result
}
}
代碼: 選擇全部
class MasterViewController: UITableViewController {
var datasource: [ViewModel] = [ViewModel]()
override func viewDidLoad() {
super.viewDidLoad()
datasource.append(ViewModel(a: "testA", b: "testB"))
datasource.append(ViewModel(a: "testC", b: "testD"))
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return datasource.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
var cust: ViewModel
cust = datasource[indexPath.row]
cell.textLabel?.text = cust.A
cell.detailTextLabel?.text = cust.B
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if let identifier = segue.identifier
{
switch identifier
{
case "ToDetail":
let controller = segue.destinationViewController as! DetailViewController
if let indexPath = self.tableView.indexPathForCell(sender as! UITableViewCell){
controller.selectedViewModel = datasource[indexPath.row]
}
default: break
}
}
}
}
代碼: 選擇全部
class DetailViewController: UITableViewController {
var selectedViewModel: ViewModel?
var datasource = [Objects]()
override func viewDidLoad() {
super.viewDidLoad()
self.datasource = ViewModel.Info(selectedViewModel!)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return datasource.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datasource[section].sectionObjects.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
// Configure the cell...
cell.textLabel?.text = datasource[indexPath.section].sectionObjects[indexPath.row]
cell.textLabel?.numberOfLines = 0
cell.textLabel?.lineBreakMode = .ByWordWrapping // or NSLineBreakMode.ByWordWrapping
return cell
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return datasource[section].sectionName
}
}