user4579505
user4579505

Reputation:

How to set index in tableview in swift

I'm failing to properly understand how to convert this into Swift. Could someone help me? I clearly need to improve my Objective-C understanding :(

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 is full of swift code check it this is optional not mentioned it As swift is in some regards more functional than object-oriented (and Arrays are structs, not objects), use the function "find" to operate on the array, which returns an optional value, so be prepared to handle a nil value:

Upvotes: 4

Views: 6940

Answers (2)

Memon Irshad
Memon Irshad

Reputation: 972

 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

Upvotes: 1

Leo Dabus
Leo Dabus

Reputation: 236305

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
}

Upvotes: 0

Related Questions