gpt4 book ai didi

arrays - 按多个数组对 Tableview 行进行排序

转载 作者:行者123 更新时间:2023-11-28 08:04:09 25 4
gpt4 key购买 nike

我目前正在尝试按每行标签上的文字对我的表格 View 进行排序。每行将包含 3 个内容:颜色、动物类型和动物名称。我对如何组织表格有一个特定的顺序,但根据项目的加载方式,行的顺序可以是任何顺序(问题)。

我想用这个数组按颜色对这些表进行排序:colorArray = ["red", "blue", "yellow", "green", "orange", "purple"] .这意味着所有红色动物将排在第一位,而所有绿色动物将排在中间等等。第一个问题是我不知道如何通过另一个字符串数组对数组进行排序 第二个问题 是我需要其他两个数组(动物和动物名称)根据颜色数组更改它们的顺序,以便正确的动物和它们的名称具有正确的颜色.

示例:如果颜色数组加载为 blue, green, orange, red并将动物数组加载为 dog, cow, cat, monkey ,然后我需要将这两个数组排序为 red, blue, green orangemonkey, 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
}

最佳答案

切勿使用多个数组作为数据源。它们容易出错并且维护起来很烦人。使用自定义结构,这是一种面向对象的语言。

  • 创建这个结构,颜色字符串将映射到一个索引,反之亦然

    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
    }
  • 现在按 colorIndexanimals 进行排序,您将获得所需的顺序

    animals.sort{ $0.colorIndex < $1.colorIndex }

关于arrays - 按多个数组对 Tableview 行进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45681007/

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