簡體   English   中英

訂單集合查看基於customObject屬性的單元格 - Swift 4 -Xcode 9

[英]Order Collection View Cells based on customObject Property - Swift 4 -Xcode 9

我目前正在使用AlamofireImage從firebase下載我的圖像。 圖像進入集合視圖,圖像的順序很重要。 我做了所以每張照片都有一個標簽。

這是我照片的課程:

class businessPhoto {

var image = UIImage()
var url = String()
var tag = Int()

}

我知道Alamofire是異步的。 下載完成后,我無法對數組進行排序。

(實際上完成下載與完成for循環有關)

這是我得到的:

在此輸入圖像描述

這就是我需要的:

在此輸入圖像描述

我的代碼:

    // Creates an Array of URLS
func createURLArray() {

    if selectedBusiness.image1URL?.isEmpty == false {

        imageURLS.append(selectedBusiness.image1URL!)

    } else {

        let placeholder = "--"
        imageURLS.append(placeholder)

    }

    if selectedBusiness.image2URL?.isEmpty == false {

        imageURLS.append(selectedBusiness.image2URL!)

    } else {

        let placeholder = "--"
        imageURLS.append(placeholder)

    }

    if selectedBusiness.image3URL?.isEmpty == false {

        imageURLS.append(selectedBusiness.image3URL!)

    } else {

        let placeholder = "--"
        imageURLS.append(placeholder)

    }

    if selectedBusiness.image4URL?.isEmpty == false {

        imageURLS.append(selectedBusiness.image4URL!)

    } else {

        let placeholder = "--"
        imageURLS.append(placeholder)

    }

    if selectedBusiness.image5URL?.isEmpty == false {

        imageURLS.append(selectedBusiness.image5URL!)

    } else {

        let placeholder = "--"
        imageURLS.append(placeholder)

    }

    if selectedBusiness.image6URL?.isEmpty == false {

        imageURLS.append(selectedBusiness.image6URL!)

    } else {

        let placeholder = "--"
        imageURLS.append(placeholder)

    }

    if selectedBusiness.image7URL?.isEmpty == false {

        imageURLS.append(selectedBusiness.image7URL!)

    } else {

        let placeholder = "--"
        imageURLS.append(placeholder)

    }
    downloadImages(URLArray: imageURLS)

}





// ** Image Download
func downloadImages(URLArray: [String]) {

    for stringURL in URLArray {

        // Temporary Object 
        let object = businessPhoto()

        //Sets tag for object
        if stringURL == imageURLS[0] {

            object.tag = 1

        } else if stringURL == imageURLS[1] {

            object.tag = 2

        } else if stringURL == imageURLS[2] {

            object.tag = 3

        } else if stringURL == imageURLS[3] {

            object.tag = 4

        } else if stringURL == imageURLS[4] {

            object.tag = 5

        } else if stringURL == imageURLS[5] {

            object.tag = 6

        } else if stringURL == imageURLS[6] {

            object.tag = 7
        }

        if stringURL == "--" {


        } else {

            // Downloads Image 
            let url = URL(string: stringURL)
            Alamofire.request(url!).responseImage { response in


                if let image = response.result.value {

                    DispatchQueue.main.async {
                        object.image = image
                        object.url = stringURL
                        self.currentImages.append(object)
                        self.photoCollectionView.reloadData()
                    }


                } else {


                    object.image = #imageLiteral(resourceName: "image-failed")
                    object.url = "--"
                    self.currentImages.append(object)
                    //self.photoCollectionView.reloadData()
                }
            }

        }


    }
    //completion()
}

//This function should update the collection view to show the photos sorted by their tag but does nothing
 func updateCollectionView() {

    print("UPDATING COLLECTION VIEW")
    print(currentImages)
    currentImages.sort(by: { $0.tag > $1.tag })
    self.photoCollectionView.reloadData()


}

在我看來,要使它有正確的順序。 按照我的步驟。

  • 如果不存在,則在方法downloadImages開始時使用7個空UIImage創建self.currentImages
  • 使用self.currentImages.insert(object, atIndex:object.tag)而不是self.currentImages.append(object)

     Alamofire.request(url!).responseImage { response in if let image = response.result.value { DispatchQueue.main.async { object.image = image object.url = stringURL self.currentImages.insert(object, atIndex:object.tag) self.photoCollectionView.reloadData() } } else { object.image = #imageLiteral(resourceName: "image-failed") object.url = "--" self.currentImages.insert(object, atIndex:object.tag) //self.photoCollectionView.reloadData() } } } 

編輯

func checkAndAddUrl(url : String) {
  if url?.isEmpty == false {
    imageURLS.append(url!)
  } else {
    let placeholder = "--"
    imageURLS.append(placeholder)
  }
}

func createURLArray() {

  checkAndAddUrl(selectedBusiness.image1URL)
  checkAndAddUrl(selectedBusiness.image2URL)
  checkAndAddUrl(selectedBusiness.image3URL)
  checkAndAddUrl(selectedBusiness.image4URL)
  checkAndAddUrl(selectedBusiness.image5URL)
  checkAndAddUrl(selectedBusiness.image6URL)
  checkAndAddUrl(selectedBusiness.image7URL)

  downloadImages(URLArray: imageURLS)

}





// ** Image Download
func downloadImages(URLArray: [String]) {

  for stringURL in URLArray {

    // Temporary Object
    let object = businessPhoto()

    object.tag = URLArray.indexOf(stringURL)

    if stringURL == "--" {


    } else {

      // Downloads Image
      let url = URL(string: stringURL)
      Alamofire.request(url!).responseImage { response in


        if let image = response.result.value {

          DispatchQueue.main.async {
            object.image = image
            object.url = stringURL
            self.currentImages.append(object)
            self.photoCollectionView.reloadData()
          }


        } else {


          object.image = #imageLiteral(resourceName: "image-failed")
          object.url = "--"
          self.currentImages.append(object)
          //self.photoCollectionView.reloadData()
        }
      }

    }


  }
  //completion()
}

//This function should update the collection view to show the photos sorted by their tag but does nothing
func updateCollectionView() {

  print("UPDATING COLLECTION VIEW")
  print(currentImages)
  currentImages.sort(by: { $0.tag > $1.tag })
  self.photoCollectionView.reloadData()


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM