uitableview - Table View UI error: Swift -
on last question asked code error in animal table view project, finished initial coding, ui turned strange. missing first letter of each animal name , table view prototype cell. example, amel should camel , hinoceros should rhinoceros. bug code here?
import uikit class animaltableviewcontroller: uitableviewcontroller { var animalsdict = [string: [string]] () var animalselectiontitles = [string] () let animals = ["bear", "black swan", "buffalo", "camel", "cockatoo", "dog", "donkey", "emu", "giraffe", "greater rhea", "hippopotamus", "horse", "koala", "lion", "llama", "manatus", "meerkat", "panda", "peacock", "pig", "platypus", "polar bear", "rhinoceros", "seagull", "tasmania devil", "whale", "whale shark", "wombat"] func createanimaldict() { animal in animals { let animalkey = animal.substringfromindex(advance(animal.startindex, 1)) if var animalvalues = animalsdict[animalkey] { animalvalues.append(animal) animalsdict[animalkey] = animalvalues } else { animalsdict[animalkey] = [animal] } } animalselectiontitles = [string] (animalsdict.keys) animalselectiontitles.sort({ $0 < $1}) animalselectiontitles.sort( { (s1:string, s2:string) -> bool in return s1 < s2 }) } override func viewdidload() { super.viewdidload() createanimaldict() } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } // mark: - table view data source override func numberofsectionsintableview(tableview: uitableview) -> int { // return number of sections. return animalselectiontitles.count } override func tableview(tableview: uitableview, titleforheaderinsection section:int) -> string? { // return number of rows in section. return animalselectiontitles[section] } override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as! uitableviewcell let animalkey = animalselectiontitles[indexpath.section] if let animalvalues = animalsdict[animalkey] { cell.textlabel?.text = animalvalues[indexpath.row] let imagefilename = animalvalues[indexpath.row].lowercasestring.stringbyreplacingoccurrencesofstring("", withstring: "_", options: nil, range: nil) cell.imageview?.image = uiimage(named:imagefilename) } return cell }
}
so far can error in createanimaldict()
method. in line
let animalkey = animal.substringfromindex(advance(animal.startindex, 1))
exchange second parameter in advance 0 be:
let animalkey = animal.substringfromindex(advance(animal.startindex, 0))
in fact don't know trying do.
Comments
Post a Comment