gpt4 book ai didi

swift - 添加与indexPath一样多的数据 - Swift

转载 作者:行者123 更新时间:2023-11-30 10:29:16 25 4
gpt4 key购买 nike

我从数据库中提取数据,将我获取的数据分配给LINKSTRING。我分配的数据是链接,我正在使用此链接查看照片。例如,我已分配给 LINKSTRING 5 个数据,如何将这 5 个数据分配给 photos 变量。我想在 LINKSTRING 中添加最多 x 张照片。示例:

self.photos = [CustomPhotoModel (imageURL: URL (string: "\ (self.LINKSTRING [0])"), thumbnailImageURL: URL (string: "\ (self.LINKSTRING [0]))"))] self.photos = [CustomPhotoModel (imageURL: URL (string: "\ (self.LINKSTRING [1])"), thumbnailImageURL: URL (string: "\ (self.LINKSTRING [1]))"))] 

如何在表单中分配变量?

import UIKit
import INSPhotoGallery

class CustomModelViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!

lazy var photos = [CustomPhotoModel]()
var LINKSTRING : [String] = []

override func viewDidLoad() {
super.viewDidLoad()
fillWithYORUM()
collectionView.delegate = self
collectionView.dataSource = self
}
func fillWithYORUM() {
let client = SQLClient.sharedInstance()!
client.connect("...", username: "...", password: "...", database: "...") { success in
client.execute("SELECT ... FROM ...", completion: { (_ results: ([Any]?)) in
var gifsa: [String] = []
for table in results as! [[[String:AnyObject]]] {
for row in table {
for (_, value) in row {
if let intVal = value as? String {
gifsa.append(String(intVal)) }} }}
DispatchQueue.main.async {
self.photos = [CustomPhotoModel(imageURL: URL(string: "\(LINKSTRING)"), thumbnailImageURL: URL(string: "\(LINKSTRING)"))]

self.collectionView.reloadData()
}
client.disconnect()
}) } }
}


extension CustomModelViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ExampleCollectionViewCell", for: indexPath) as! ExampleCollectionViewCell
cell.populateWithPhoto(photos[(indexPath as NSIndexPath).row])

return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return photos.count
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! ExampleCollectionViewCell
let currentPhoto = photos[(indexPath as NSIndexPath).row]
let galleryPreview = INSPhotosViewController(photos: photos, initialPhoto: currentPhoto, referenceView: cell)

galleryPreview.referenceViewForPhotoWhenDismissingHandler = { [weak self] photo in
if let index = self?.photos.firstIndex(where: {$0 === photo}) {
let indexPath = IndexPath(item: index, section: 0)
let cell = collectionView.cellForItem(at: indexPath) as? ExampleCollectionViewCell
return cell
}
return nil
}
present(galleryPreview, animated: true, completion: nil)
}
}

最佳答案

您只需将数据附加到照片数组中,如下所示:

if LINKSTRING.count > 0 {
for photoUrl in LINKSTRING {
let data = CustomPhotoModel (imageURL: URL (string: photoUrl), thumbnailImageURL: URL (string: photoUrl)]
self.photos.append(data)
}
}

关于swift - 添加与indexPath一样多的数据 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59453833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com