我想重复加载collectionView Cell。
我想制作一个动态单元格,当加载20个单元格时,我想通过再次在下面填充20个单元格来制作40个单元格。
API.requestBookCategory(bookCategory: 9, bookAddPoint: 0, completionHandler: handleBooksCategory(books:error:))
func handleBooksCategory(books: Books?, error: Error?) {
self.booksCategory = books
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
此代码从bookCategory
9中的20本书中检索信息。
导入1到20本书信息时,请插入bookAddPoint
0。
此后,我们将书籍信息插入bookCatagory
变量中,并使用bookCategory
在单元格中显示书籍信息数据。
extension ViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return booksCategory?.books.count ?? 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView
.dequeueReusableCell(withReuseIdentifier: "MainBookCell", for: indexPath) as! MainBookCell
func handleImageResponse() {
guard let imageURL = URL(string: booksCategory?.books[indexPath.row].bookImage ?? "") else {
return
}
API.requestImageFile(url: imageURL, completionHandler: handleImageFileResponse(image:error:))
}
func handleImageFileResponse(image: UIImage?, error: Error?) {
DispatchQueue.main.async {
cell.bookImageView.image = image
}
}
cell.bookTitleLabel.text = booksCategory?.books[indexPath.row].bookTitle
cell.bookWriterLabel.text = booksCategory?.books[indexPath.row].authorName
handleImageResponse()
return cell
}
}
我一直进行到这里,创建了一个显示20本书信息的单元格。
如果创建了显示第20本书信息的单元格,则
API.requestBookCategory (bookCategory: 9, bookAddPoint: 20, completionHandler: handleBooksCategory (books: error :))
我想像上面的代码一样运行将bookAddPoint
从0修改为20的代码,以创建一个显示第21至40本书信息的单元格。我该怎么办?
答案 0 :(得分:1)
在viewControoller中创建变量
var bookAddPoint = 0
var pageLength = 20
func callNextPageData(){
bookAddPoint = booksCategory?.books.count + 1
API.requestBookCategory(bookCategory: 9, bookAddPoint: bookAddPoint, completionHandler: handleBooksCategory(books:error:))
}
更改
func handleBooksCategory(books: Books?, error: Error?) {
for book in books{
self.booksCategory.appen(book)
}
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
更改 cellforItem
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView
.dequeueReusableCell(withReuseIdentifier: "MainBookCell", for: indexPath) as! MainBookCell
func handleImageResponse() {
guard let imageURL = URL(string: booksCategory?.books[indexPath.row].bookImage ?? "") else {
return
}
API.requestImageFile(url: imageURL, completionHandler: handleImageFileResponse(image:error:))
}
func handleImageFileResponse(image: UIImage?, error: Error?) {
DispatchQueue.main.async {
cell.bookImageView.image = image
}
}
cell.bookTitleLabel.text = booksCategory?.books[indexPath.row].bookTitle
cell.bookWriterLabel.text = booksCategory?.books[indexPath.row].authorName
handleImageResponse()
if indexPath.row == self.booksCategory.count-3 // it will load records before you reach end point automatically you can increase or decrease number
{
callNextPageData()
}
return cell
}
注意::如果要在加载更多按钮时执行此操作,只需在按钮操作或tableView页脚操作中调用callNextPageData()。