1 頁 (共 1 頁)

UITableView : Sectioning and Indexing

發表於 : 週三 12月 09, 2015 3:18 pm
rusli
SectioningAndIndexing.zip
(28.69 KiB) 已下載 28 次


代碼: 選擇全部

import UIKit

class Model: NSObject
{
    var Name: String
    var City: String
   
    var section: Int?
   
    init(name: String, city: String)
    {
        self.Name = name
        self.City = city
    }
}


代碼: 選擇全部

class ViewController: UITableViewController
{
    var DataSource: [Model] = [Model]()

    // custom type to represent table sections
    class Section {
        var items: [Model] = []
       
        func add(item: Model) {
            self.items.append(item)
        }
    }
   
    // `UIKit` convenience class for sectioning a table
    let collation = UILocalizedIndexedCollation.currentCollation() as UILocalizedIndexedCollation

    // table sections
    var sections: [Section] {
        // return if already initialized
        if self._sections != nil {
            return self._sections!
        }
       
        // create users from the name list
        let items: [Model] = MapDatasourceToIndex()
       
        // create empty sections
        let sections: [Section] = CreateEmptySections()
       
        // put each item in a section
        PutEachItemInSection(items, target: sections)
       
        // sort each section
        SectionItemsSort(sections)
       
        self._sections = sections
       
        return self._sections!
    }

    var _sections: [Section]?

    func SectionItemsSort(target: [Section])
    {
        for section in target {
            section.items = self.collation.sortedArrayFromArray(section.items, collationStringSelector: "Name") as! [Model]
        }
    }
   
    func PutEachItemInSection(source: [Model], target: [Section])
    {
        for item in source {
            target[item.section!].add(item)
        }
    }
   
    func CreateEmptySections() -> [Section]
    {
        var sections = [Section]()
        for _ in 0..<self.collation.sectionIndexTitles.count {
            sections.append(Section())
        }
       
        return sections
    }

    func MapDatasourceToIndex() -> [Model]
    {
        let dataSource = DataSource

        // create users from the name list
        return dataSource.map { model in
            model.section = self.collation.sectionForObject(model, collationStringSelector: "Name")
            return model
        }
    }
   
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
       
        DataSource.append(Model(name: "A_Rusli1", city: "City1"))
        DataSource.append(Model(name: "A_Rusli2", city: "City2"))
        DataSource.append(Model(name: "B_Rusli1", city: "City3"))
        DataSource.append(Model(name: "B_Rusli2", city: "City4"))
        DataSource.append(Model(name: "C_Rusli1", city: "City5"))
        DataSource.append(Model(name: "C_Rusli2", city: "City6"))
        DataSource.append(Model(name: "C_Rusli3", city: "City7"))
    }

    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 self.sections.count
    }
   
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return self.sections[section].items.count
    }
   
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
       
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
        let cust = self.sections[indexPath.section].items[indexPath.row]
       
        cell.textLabel?.text = cust.Name
        cell.detailTextLabel?.text = cust.City
        return cell
    }
   
    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String
    {
        // do not display empty `Section`s
        if !self.sections[section].items.isEmpty {
            return self.collation.sectionTitles[section] as String
        }
       
        return ""
    }
   
    override func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]?
    {
        return self.collation.sectionIndexTitles
    }
   
    override func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int
    {
        return self.collation.sectionForSectionIndexTitleAtIndex(index)
    }
}