【问题标题】:Sorting list into sections for tableview index将列表排序为 tableview 索引的部分
【发布时间】:2015-01-05 23:23:18
【问题描述】:

我正在寻找最佳方式来获取列表并将其分类为用于旁边的 tableView 索引的部分。在我的示例中,按 Make 的第一个字母对 Car 对象进行排序。我在想我想要一个最多有 26 个键的 Car 对象字典,但还没有工作。如果有更好的方法可以忽略,我很想听听。

任何帮助将不胜感激。

在操场示例中粘贴。

// Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"

let indexList = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

func gimmeOne(things: [String]) -> String {
    let randomNumber = Int(arc4random()) % things.count
    let choice = things[randomNumber]
    return choice
}

func gimmeCarMake() -> String {
    let makes = ["Ford","Audi","Hennessey","Acura","Infiniti","BMW","Lexus","Jeep","Subaru","Nissan","Aston Martin","Mercedes-Benz","Porsche","Toyota","Volkswagen","Scion","Mitsubishi","Mini","Lotus","Lamborghini","Jaguar","Hyundai","Citroën","Cadillac","Bugatti","Bentley","Alfa Romeo","Dodge"]
    return gimmeOne(makes)
}

class Car:NSObject {
    var make: String

    override init() {
        self.make = gimmeCarMake()
    }
}

//create list of cars
var carList = [Car]()
for _ in 0..<10 {
    carList.append(Car())
}
carList


//sort list
carList.sort { $0.make < $1.make }
carList


func firstLetter(text: String) -> String {
    let position = 0
    let index = advance(text.startIndex, position)
    let character = "\(text[index])"
    return character
}


//goal - to be used in tableView index section
//["A"] = ["Acura","Aston Martin","Audi"]
//["B"] = ["BMW","Bentley"]
//["C"] = ["Cadillac","Citroën"]
//["D"] = ["Dodge"]

编辑: 让我们看看我是否会失去剩下的声望点。

我在代码中得到了最终答案,它足够通用,任何人都可以通过一些快速更改来使用它。如果您需要遵循的步骤,请查看this page

希望对某人有所帮助。

//
//  ViewController.swift
//  tmpSortTable
//
// Solution from:
// http://www.pumpmybicep.com/2014/07/04/uitableview-sectioning-and-indexing/
//

import UIKit

class MyObject:NSObject {
    var thingA: String
    var thingB: String
    var section: Int?  //make room in your object for this

    class func gimmeOne(things: [String]) -> String {
        let randomNumber = Int(arc4random()) % things.count
        let choice = things[randomNumber]
        return choice
    }

    class func gimmeCarMake() -> String {
        let makes = ["Ford","Audi","Hennessey","Acura","Infiniti","BMW","Lexus","Jeep","Subaru","Nissan","Aston Martin","Mercedes-Benz","Porsche","Toyota","Volkswagen","Scion","Mitsubishi","Mini","Lotus","Lamborghini","Jaguar","Hyundai","Citroën","Cadillac","Bugatti","Bentley","Alfa Romeo","Dodge","Tesla"]
        return gimmeOne(makes)
    }

    class func gimmeCarModel() -> String {
        let model = ["F150","Mustang","LFA","S55","Raptor","Odyssey","NSX","4C","F40","F50","F70","LaFerrari","Camry","Silverado","Viper","Routan","Quattro","M100","P85 D","Stealth","Pinto","Accord"]
        return gimmeOne(model)
    }

    override init() {
        self.thingA = MyObject.gimmeCarMake()
        self.thingB = MyObject.gimmeCarModel()
    }

    override var description: String {
        return "\n\(thingA) \(thingB) in section \(section)"
    }
}



class ViewController: UITableViewController {

    //setup
    var data = [MyObject]()  //main list of data to be used

    override func viewDidLoad() {
        super.viewDidLoad()
        createData()
    }

    //create data or fill data any way you want
    func createData() {
        for _ in 0..<50 {
            data.append(MyObject())
        }
    }

    //now the important section stuff
    class Section {
        var items: [MyObject] = []

        func addItem(item: MyObject) {
            self.items.append(item)
        }

    }

    // `UIKit` convenience class for sectioning a table
    let collation = UILocalizedIndexedCollation.currentCollation() as UILocalizedIndexedCollation
    var sections: [Section] {
        if self._sections != nil {
            return self._sections!
        }

        //identify item to section
        var items: [MyObject] = data.map { dataItem in
            dataItem.section = self.collation.sectionForObject(dataItem, collationStringSelector: "thingA")
            return dataItem
        }

        //create empty sections
        var sections = [Section]()
        for _ in 0..<self.collation.sectionIndexTitles.count {
            sections.append(Section())
        }

        //put each item into a section
        for item in items {
            sections[item.section!].addItem(item)
        }

        //sort each section
        for section in sections {
            section.items = self.collation.sortedArrayFromArray(section.items, collationStringSelector: "thingA") as [MyObject]
        }

        self._sections = sections
        return self._sections!
    }
    var _sections: [Section]?





    //TableView data source
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        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 item = self.sections[indexPath.section].items[indexPath.row]
        let cell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell", forIndexPath: indexPath) as UITableViewCell
        cell.textLabel.text = "\(item.thingA) \(item.thingB)"
        return cell
    }

    /* section headers appear above each `UITableView` section */
    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 ""
    }

    /* section index titles displayed to the right of the `UITableView` */
    override func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject] {
        return self.collation.sectionIndexTitles
    }

    override func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
        return self.collation.sectionForSectionIndexTitleAtIndex(index)
    }

}

【问题讨论】:

    标签: ios arrays sorting dictionary swift


    【解决方案1】:

    在 Swift 中查看本教程 Indexed Table View in Swift。您将希望使用 UILocalizedIndexedCollat​​ion 便利类来创建索引,而不是手动或以编程方式进行。它使用您的项目本地化列表中定义的语言环境。这样您就不必自己创建列表,也不必编写代码来创建列表,并且在添加/删除更多条目时需要更少的维护。这也将能够利用您最终在应用程序中进行的任何本地化。

    您还可以查看UILocalized​Indexed​Collation on NSHipster 了解有关使用该课程的更多详细信息。

    【讨论】:

    • 网址损坏。请检查。
    • @SazzadHissainKhan 我链接的教程现在已经消失了。我添加了一个替代品。
    【解决方案2】:

    您可以使用下面的代码并执行 buildIndex(words) 来获得如下内容:

    [(A, [讴歌, 奥斯顿马丁, 奥迪]), (B,[宝马,宾利]), (C, [凯迪拉克, 雪铁龙]), …… ]

    import Foundation
    
    let makes = ["Ford","Audi","Hennessey","Acura","Infiniti","BMW","Lexus","Jeep","Subaru","Nissan","Aston Martin","Mercedes-Benz","Porsche","Toyota","Volkswagen","Scion","Mitsubishi","Mini","Lotus","Lamborghini","Jaguar","Hyundai","Citroën","Cadillac","Bugatti","Bentley","Alfa Romeo","Dodge"]
    
    let words = makes.sorted({$0 < $1})
    
    typealias Entry = (Character, [String])
    
    func distinct<T: Equatable>(source: [T]) -> [T] {
      var unique = [T]()
      for item in source {
        if !contains(unique, item) {
          unique.append(item)
        }
      }
      return unique
    }
    
    func buildIndex(words: [String]) -> [Entry] {
      let letters = words.map {
        (word) -> Character in
        Character(word.substringToIndex(advance(word.startIndex, 1)
          ).uppercaseString)
      }
      let distinctLetters = distinct(letters)
    
      return distinctLetters.map {
        (letter) -> Entry in
        return (letter, words.filter {
          (word) -> Bool in
         Character(word.substringToIndex(advance(word.startIndex, 1)
           ).uppercaseString) == letter
        })
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-12-25
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      • 2016-09-10
      • 1970-01-01
      • 2021-01-27
      相关资源
      最近更新 更多