2017-08-14 84 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