我目前正在尝试按照每行中标签上的单词对我的tableview进行排序。每行将有三件事:一种颜色,一种动物类型和动物的名字。我在想如何组织表格时有特定的顺序,但取决于项目如何加载,行的顺序可以是任何事情(问题)。通过多个数组排序Tableview行
我想按这个数组的颜色排序这些表:colorArray = ["red", "blue", "yellow", "green", "orange", "purple"]
。这意味着所有的红色动物将首先,而所有的绿色的动物将在第一等。第一个问题是我不知道如何排序一个数组由另一个字符串数组。 第二个问题是我需要其他两个数组(动物和动物名称)根据颜色数组来改变它们的顺序,所以正确的动物和他们的名字将会使用正确的颜色。
实施例:如果彩色阵列中装载样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
}
使用** **一个自定义的结构或类,而不是多个阵列。它使生活变得更加轻松,并且可以解决您的问题。对于颜色使用索引来获取自定义订单。 – vadian