我目前正在尝试按照每行标签上的字词对tableview进行排序。每行将有3个东西:颜色,动物类型和动物的名称。关于如何组织表,我有一个特定的顺序,但根据项的加载方式,行的顺序可以是任何(问题)。
我想通过这个数组按颜色对这些表进行排序:colorArray = ["red", "blue", "yellow", "green", "orange", "purple"]
。这意味着所有的红色动物都是第一个,而所有的绿色动物都在中间等。 第一个问题是我不知道如何用另一个字符串数组对数组进行排序< / strong>即可。 第二个问题是我需要另外两个数组(动物和动物名称)根据颜色数组改变它们的顺序所以正确的动物和它们的名字&#39;将使用正确的颜色。
示例:如果颜色数组以blue, green, orange, red
加载,动物数组加载dog, cow, cat, monkey
,那么我将需要这两个数组分为red, blue, green orange
和monkey, dog, cow, cat
。这是所有动物都有正确的颜色。 如何解决这两个问题?我已将当前代码复制到底部:
func loadAnimals() {
let animalQuery = PFQuery(className: "Animals")
animalQuery.whereKey("userID", equalTo: PFUser.current()?.objectId! ?? String()) //getting which 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)
for object in objects! {
self.colorArray.append(object.value(forKey: "colorType") as! String) // add data to arrays
self.animalNameArray.append(object.value(forKey: "animalName") as! String) // add data to arrays
self.animalTypeArray.append(object.value(forKey: "animalType") as! String) // add data to 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 to color cell
//Adds the animal information in the cells
cell.colorType.text = colorArray[indexPath.row]
cell.animalName.text = animalNameArray[indexPath.row]
cell.animalType.text = animalTypeArray[indexPath.row]
return cell
}
答案 0 :(得分:0)
绝不要使用多个数组作为数据源。它们容易出错且难以维护。使用自定义结构,这是一种面向对象的语言。
创建此结构,颜色字符串将映射到索引,反之亦然
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]
}
}
此数据源数组
var animals = [Animal]()
将loadAnimals()
替换为
func loadAnimals() {
let animalQuery = PFQuery(className: "Animals")
animalQuery.whereKey("userID", equalTo: PFUser.current()?.objectId! ?? String()) //getting which user
animalQuery.limit = 10
animalQuery.findObjectsInBackground { (objects, error) in
if error == nil {
animals.removeAll()
for 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())
}
}
}
cellForRow
和
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! AnimalCell //connects to color cell
//Adds the animal information in the cells
let animal = animals[indexPath.row]
cell.colorType.text = animal.color
cell.animalName.text = animal.name
cell.animalType.text = animal.type
return cell
}
现在按animals
排序colorIndex
,即可获得所需订单
animals.sort{ $0.colorIndex < $1.colorIndex }