2017-12-21 16 views
0

저는 KolodaView를 구현하고 있습니다 : https://github.com/Yalantis/Koloda. viewForCardAt 함수는 UIView을 반환하고 내 UIView에는 다운로드해야하는 이미지가 있습니다. 문제는 함수 자체가 UIView의 반환 유형을 예상하지만 setupCard 메서드의 완료 블록이 실행되면 알 수있는 방법이 없으므로 완료 블록에서 대신 빈 을 반환 할 수 있습니다. . 완료 블록에 return a을 추가하려고 시도했지만 허용되지 않습니다. 완료 블록이 실행 된 후에 만 ​​카드가 반환되도록 아래 코드를 어떻게 변경합니까?Completion Block Swift에서 돌아 가기

func koloda(_ koloda: KolodaView, viewForCardAt index: Int) -> UIView { 

    var a = FlatCard() 
    if let listings = all_listings { 
     if index < listings.count { 
      setupCard(index: index, listings: listings, { (complete, card) in 
       if (complete) { 
        a = card 
       } 
      }) 
      return a 
     } 
    } 
    return a 
} 

func setupCard(index: Int, listings : [Listing], _ completionHandler: @escaping (_ complete: Bool, _ card : FlatCard) ->()) ->(){ 

    let curr_card = FlatCard() 

    if let main_photo_url = listings[index].pic1url { 
     URLSession.shared.dataTask(with: main_photo_url, completionHandler: { (data, response, error) in 

      if (error != nil) { 
       print(error) 
       return 
      } 

      DispatchQueue.main.async { 
       curr_card.mainFlatImage = UIImage(data: data!) 
      } 
     }) 
     completionHandler(true,curr_card) 
     return 
    } else { 
     completionHandler(true,curr_card) 
     return 
    } 
} 
+0

FlatCard''무엇인가? –

답변

1

준비되기 전에는 반환 할 수 없습니다.

개인적으로, 이미지 자체를 다운로드하고 완료되면 자체보기를 업데이트 할 수 있도록 개인적으로 FlatCard를 업데이트합니다. 그런 다음

class FlatView: UIView { 

    var imageURL: URL? { 
     didSet { 
      if let imageURL = newValue { 
       // download image, if success set the image on the imageView 
      } 
     } 
    } 
} 

의 라인을 따라

뭔가 당신이 다른 함수에서 할 필요가있는 모든 ...

func koloda(_ koloda: KolodaView, viewForCardAt index: Int) -> UIView { 

    var a = FlatCard() 
    if let listings = all_listings { 
     if index < listings.count { 
      a.imageURL = listings[index].pic1url 
     } 
    } 
    return a 
}