gpt4 book ai didi

ios - 如何在 tableview 中快速设置索引

转载 作者:搜寻专家 更新时间:2023-10-31 19:39:45 25 4
gpt4 key购买 nike

我没能正确理解如何将其转换为 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 代码检查它这是可选的,没有提到它由于 swift 在某些方面比面向对象更实用(数组是结构,而不是对象),请使用函数“find”对数组进行操作,它返回一个可选值,因此请准备好处理 nil 值:

最佳答案

 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)
}

希望能成功

关于ios - 如何在 tableview 中快速设置索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28778718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com