我正在尝试将UITableView
嵌入UICollectionViewCell
中。我正在设法加载所有内容。但是,当确实加载它时,似乎随机使用了UITableView
列表项的最后一个加载值。我的DebugPrint
输出似乎在引用TableView之前就加载了所有CollectionViewCells
。设置CollectionViewCells后,它将使用剩下的任何值填充TableView。如何解决此问题,以便使用正确的值?我怀疑这是我使用IndexPath
的方式,但是我对Swift还是很陌生。
填充UICollectionView
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: releaseIdentifier, for: indexPath) as! ReleaseCollectionViewCell
let vid: VideoItem
vid = Video[indexPath.item]
let rating = vid.Rating
let denominator = 2.0
let roundedRating = round(rating*denominator )/denominator
if vid.SiteVideoID != 0 {
cell.ReleaseImage.setImageFromURl(stringImageUrl: "\(file.BaseURL)/sitePictures/\(GetFilePath(ID: String(vid.SiteVideoID)))/\(String(vid.SiteVideoID)).jpg")
} else {
cell.ReleaseImage.setImageFromURl(stringImageUrl: "\(file.BaseURL)/pictures/\(GetFilePath(ID: String(vid.VideoID)))/\(String(vid.VideoID))_001.png")
}
cell.ReleaseDate.text = vid.ReleaseDate
cell.ReleaseTitle.text = vid.Title
cell.SiteLogo.setImageFromURl(stringImageUrl: "\(file.BaseURL)/siteLogos/" + String(vid.SiteID) + ".png")
if vid.VideoID == 0 {
cell.ReleaseImage.alpha = 0.5
} else {
cell.ReleaseImage.alpha = 1.0
}
indexPathItem = -1
indexPathItem = indexPath.item
debugPrint(indexPathItem)
cell.TagsView.delegate = self
cell.TagsView.dataSource = self
return cell
}
以及用于填充TableView
extension MiscCollectionViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if indexPathItem != -1 {
return Video[indexPathItem].Tags.count
} else {
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let tags = tableView.dequeueReusableCell(withIdentifier: "Tag", for: indexPath) as! TagTableViewCell
debugPrint(String(indexPathItem) + " " + Video[indexPathItem].Tags[indexPath.row])
tags.TagLabel.text = String(indexPathItem) //Video[indexPathItem].Tags[indexPath.row]
return tags
}
}