我有一个collectionview,其中有(hdfs dfsadmin -metasave <filename>
)个部分,每个部分中都有两个项目。我必须使用section才能为每一行设置页脚。
问题在于,在items.count / 2
方法中,cellForItemAt indexPath
的值未按正确的顺序返回。
这是我的indexPath.section
代码:
cellForItemAt indexPath
和向下滚动打印方法的结果是:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LibraryCell",for: indexPath) as! LibraryCell
cell.cover.image = nil
var position = 0
if indexPath.row == 0 {
position = indexPath.section * 2
}else {
position = (indexPath.section * 2) + 1
}
print("section: " + String(indexPath.section) + " row: " + String(indexPath.row) + " item: " + String(indexPath.item))
let book = items[position] as! MyBooksTable
if let url = URL(string: String(book.coverUrl ?? "")){
cell.cover.kf.setImage(with: url, placeholder: UIImage(named: "cover_placeholder"), options: [.transition(.fade(0.2))], progressBlock: nil, completionHandler: nil)
}
return cell
}
您可以看到12以后有7 .... 21以后有16,依此类推... 这会导致错误的数据呈现,而我无法弄清原因。