How to set index in tableview in swift

I do not understand how to convert this to Swift. Can someone help me? I clearly need to improve my understanding of Objective-C :(

var animals : [String : [String]] =
["B" : ["Bear", "Black Swan", "Buffalo"],
    "C" : ["Camel", "Cockatoo"],
    "D" : ["Dog", "Donkey"],
    "E" : ["Emu"],
    "G" : ["Giraffe", "Greater Rhea"],
    "H" : ["Hippopotamus", "Horse"],
    "K" : ["Koala"],
    "L" : ["Lion", "Llama"],
    "M" : ["Manatus", "Meerkat"],
    "P" : ["Panda", "Peacock", "Pig", "Platypus", "Polar Bear"],
    "R" : ["Rhinoceros"],
    "S" : ["Seagull"],
    "T" : ["Tasmania Devil"],
    "W" : ["Whale", "Whale Shark", "Wombat"]]

var animalSection = [String]()
var rev = [String]()

var animalIndexTitles = ["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"];

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a ni
    animalSection = animals.keys.array
    rev = sorted(animalSection, { (s1, s2) -> Bool in
        return s1 <= s2
    })
    println(rev)

}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return animalSection.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

  var sectionTitle = rev[section]   // String
    var sectionAnimals : [String] = animals[sectionTitle]! // String Array
    return sectionAnimals.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: AnyObject = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
    var sectionTitle = rev[indexPath.section]
    var sectionAnimals : [String] = animals[sectionTitle]!
    var animal = sectionAnimals[indexPath.row]
    cell.textLabel.text = animal
    return cell as UITableViewCell

    }

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return rev[section]
}

func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]! {
    return animalIndexTitles
}

func tableView(tableView: UITableView, sectionForSectionIndexTitle      title: String, atIndex index: Int) -> Int
{
 return 0
}

here is full of fast code, check it is not necessarily mentioned since swift is in some respects more functional than object-oriented (and arrays are structures, not objects), use the find function to work with an array that returns an optional value, so be prepared to handle the nil value: / p>

+4
source share
2 answers
 rev  = sorted(animalSection, { (s1, s2) -> Bool in
        return s1 <= s2
    })
    println(rev)

    ns = rev

 func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int
 {
  return ns.indexOfObject(title)   
 }

I hope this work

+1
source
func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]! {
    // your code
    return ["Title A","Title B","Title C","Title D","Title E"]
}
func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
       // your code
       return 0
}
0
source

All Articles