TableViewDynamicContent

rusli
Site Admin
文章: 212
註冊時間: 週三 7月 07, 2010 9:49 pm

TableViewDynamicContent

文章rusli » 週六 12月 05, 2015 6:52 pm

TableViewDynamicContent.zip
(29.98 KiB) 已下載 21 次


代碼: 選擇全部

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
    }
}

回到「Swift」

誰在線上

正在瀏覽這個版面的使用者:沒有註冊會員 和 0 位訪客