Swift
3
Вклад в тег
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier "item", for: IndexPath) as! CustomCell
// теперь cell у нас нужная нам ячейка, настройки IBOutlets и IBActions у нас в ее кастомном классе, если
необходимо можем тут настроить какую нибудь логику. После настройки логики, нам нужно вернуть из
метода эту ячейку
return cell
}
var imageURLString: String? {
didSet {
DispatchQueue.global(qos: .utility).async { [unowned self] in
guard imageURLString != "",
let imageURLString = self.imageURLString,
let imageURL = URL(string: imageURLString) else {
DispatchQueue.main.async {
self.ImageView.image = UIImage(named: "default")
}
return
}
loadImage(imageURL: imageURL)
}
}
}
fileprivate func loadImage(imageURL: URL) {
dataTask = URLSession.shared.dataTask(with: imageURL, completionHandler: { data, response, error in
if let data = data,
let image = UIImage(data: data) {
DispatchQueue.main.async { [unowned self] in
self.ImageView.image = image
}
} else {
DispatchQueue.main.async { [unowned self] in
self.ImageView.image = UIImage(named: "default")
}
}
})
dataTask?.resume()
}
override func prepareForReuse() {
super.prepareForReuse()
dataTask?.cancel()
ImageView.image = UIImage(named: "default")
}