2017-08-14 76 views
0

我目前正在尝试按照每行中标签上的单词对我的tableview进行排序。每行将有三件事:一种颜色,一种动物类型和动物的名字。我在想如何组织表格时有特定的顺序,但取决于项目如何加载,行的顺序可以是任何事情(问题)。通过多个数组排序Tableview行

我想按这个数组的颜色排序这些表: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 
     } 
+1

使用** **一个自定义的结构或类,而不是多个阵列。它使生活变得更加轻松,并且可以解决您的问题。对于颜色使用索引来获取自定义订单。 – vadian

回答

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 
    } 
    
  • 现在,通过colorIndex排序animals,你会得到的顺序你想要

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

我在我的代码和'动物'装载中实现了这个!但我不知道如何使用'colorIndex'对它们进行排序,因为这是我第一次使用结构。 **我如何正确实施他们的分类?** – fphelp

+0

我更新了答案。 – vadian

+0

好吧,我在我的实现它,现在行正在按颜色组织!但现在还有另一个问题:**动物名称正在混淆。他们没有保持正确的颜色**我该如何解决这个问题? – fphelp