Я пытаюсь сделать неограниченный просмотр таблицы прокрутки (фид).Однако я не могу заставить просмотр таблицы вставить больше ячеек после начальной загрузки.
В моей таблице есть два типа ячеек.Ячейка таблицы представления коллекции и обычная ячейка таблицы изображения + метки.Сборный вид отображается только один раз, и он первый.
Область, которая вызывает у меня проблемы.По крайней мере, он вызывает, просто не добавляет больше ячеек.Кроме того, не уверен, откуда он знает, какой тип ячейки добавить.
extension FeedViewController: UITableViewDelegate{
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let lastElement = venueData.count
if indexPath.row == lastElement {
// handle your logic here to get more items, add it to dataSource and reload tableview
print(venueData.count)
print("last")
if(firstLoad){
firstLoad = false
return
}
let appendAmount = 10
currentOffset = currentOffset+appendAmount
let res = venueDataRequest(query:"Any",offset:10,amount:appendAmount)
venueData.append(contentsOf: res )
self.feedTableView.beginUpdates()
self.feedTableView.insertRows(at: [IndexPath(row: venueData.count-appendAmount, section: 0)], with: .automatic)
self.feedTableView.endUpdates()
}
}
}
Полный класс
import UIKit
class FeedViewController: UIViewController {
@IBOutlet weak var feedTableView: UITableView!
let popularPlaces = getPopularPlaces()
var firstLoad = true
var currentOffset = 0
var venueData = venueDataRequest(query:"Any",offset:0,amount:10)
// var venueData: [FeedVenueData] = []
override func viewDidLoad() {
super.viewDidLoad()
feedTableView.dataSource = self
feedTableView.delegate = self
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension FeedViewController: UITableViewDelegate{
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let lastElement = venueData.count
if indexPath.row == lastElement {
// handle your logic here to get more items, add it to dataSource and reload tableview
print(venueData.count)
print("last")
if(firstLoad){
firstLoad = false
return
}
let appendAmount = 10
currentOffset = currentOffset+appendAmount
let res = venueDataRequest(query:"Any",offset:10,amount:appendAmount)
venueData.append(contentsOf: res )
self.feedTableView.beginUpdates()
self.feedTableView.insertRows(at: [IndexPath(row: venueData.count-appendAmount, section: 0)], with: .automatic)
self.feedTableView.endUpdates()
}
}
}
extension FeedViewController: UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1+venueData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if(indexPath.row == 0){
let cell = tableView.dequeueReusableCell(withIdentifier: "FeedPopularPlaceCollectionViewTableViewCell", for: indexPath) as! FeedPopularPlaceCollectionViewTableViewCell
cell.setup()
return cell
}else{
let cell = tableView.dequeueReusableCell(withIdentifier: "FeedVenueTableViewCell", for: indexPath) as! FeedVenueTableViewCell
let pos = indexPath.row - 1
cell.venueName.text = venueData[pos].name
cell.venueImage.image = venueData[pos].image
print(indexPath.row)
return cell
}
}
}