swift - Sorting Tableview Rows By Multiple Arrays -
i trying sort tableview words on labels in each row. each row have 3 things: color, animal type, , name animal. have specific order in mind on how want organize table, depending on how items loaded, order of rows can (the problem).
i want sort these tables color array: colorarray = ["red", "blue", "yellow", "green", "orange", "purple"]
. mean red animals first while green ones in middle etc. first problem not know how sort array string array. second problem need other 2 arrays (animal , animal name) change order in accordance color array right animal , names' correct color.
example: if color array loaded in blue, green, orange, red
, animal array loaded in dog, cow, cat, monkey
, need need these 2 array sorted red, blue, green orange
, monkey, dog, cow, cat
. animals right color. how fix these 2 problems? have copied current code @ bottom:
func loadanimals() { let animalquery = pfquery(classname: "animals") animalquery.wherekey("userid", equalto: pfuser.current()?.objectid! ?? string()) //getting user animalquery.limit = 10 animalquery.findobjectsinbackground { (objects, error) in if error == nil { self.colorarray.removeall(keepingcapacity: false) self.animalnamearray.removeall(keepingcapacity: false) self.animaltypearray.removeall(keepingcapacity: false) object in objects! { self.colorarray.append(object.value(forkey: "colortype") as! string) // add data arrays self.animalnamearray.append(object.value(forkey: "animalname") as! string) // add data arrays self.animaltypearray.append(object.value(forkey: "animaltype") as! string) // add data arrays } self.tableview.reloaddata() } else { print(error?.localizeddescription ?? string()) } } } //places colors in rows override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecell(withidentifier: "cell") as! animalcell //connects color cell //adds animal information in cells cell.colortype.text = colorarray[indexpath.row] cell.animalname.text = animalnamearray[indexpath.row] cell.animaltype.text = animaltypearray[indexpath.row] return cell }
never use multiple arrays data source. error-prone , annoying maintain. use custom struct, object oriented language.
create struct, color string mapped index , vice versa
struct animal { static let colors = ["red", "blue", "yellow", "green", "orange", "purple"] let name : string let type : string let colorindex : int init(name : string, type : string, color : string) { self.name = name self.type = type self.colorindex = animal.colors.index(of: color)! } var color : string { return animal.colors[colorindex] } }
and data source array
var animals = [animal]()
replace
loadanimals()
withfunc loadanimals() { let animalquery = pfquery(classname: "animals") animalquery.wherekey("userid", equalto: pfuser.current()?.objectid! ?? string()) //getting user animalquery.limit = 10 animalquery.findobjectsinbackground { (objects, error) in if error == nil { animals.removeall() object in objects! { animals.append(animal(name: object["animalname") as! string, type: object["animaltype") as! string, color: object["colortype") as! string) } self.tableview.reloaddata() } else { print(error?.localizeddescription ?? string()) } } }
and
cellforrow
override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecell(withidentifier: "cell", for: indexpath) as! animalcell //connects color cell //adds animal information in cells let animal = animals[indexpath.row] cell.colortype.text = animal.color cell.animalname.text = animal.name cell.animaltype.text = animal.type return cell }
now sort
animals
colorindex
, order wantanimals.sort{ $0.colorindex < $1.colorindex }
Comments
Post a Comment