我未能正确理解如何将其转换为Swift。有人能帮助我吗?我显然需要提高我的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
}
这里充满了快速代码检查它 这是可选的,没有提到它 因为swift在某些方面比面向对象更具功能性(而Arrays是结构体,而不是对象),使用函数&#34; find&#34;对数组进行操作,返回一个可选值,所以要准备好处理一个nil值:
答案 0 :(得分:1)
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)
}
我希望这项工作
答案 1 :(得分:0)
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
}