0
在這裏我已經實現了表視圖的分頁,並且使用模型類來加載項目,但是這裏加載的項目將用新項目替換,並且每當它調用api時,都會返回新數據,舊數據將覆蓋它並僅顯示一次10個項目我第一次實施它,任何人都可以幫助我如何解決這個問題?如何在swift 3中以分頁的形式顯示錶格視圖中的所有數據?
func listCategoryDownloadJsonWithURL(listUrl: String) {
let url = URL(string: listUrl)!
print(listUrl)
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil { print(error!); return }
do {
if let jsonObj = try JSONSerialization.jsonObject(with: data!) as? [String:Any] {
self.listClassModel = ModelClass(dict: jsonObj as [String : AnyObject])
DispatchQueue.main.async {
guard let obj = self.listClassModel else { return }
let itemsCount = obj.items.count
print(itemsCount)
for i in 0..<itemsCount {
let customAttribute = obj.items[i].customAttribute
for j in 0..<customAttribute.count {
if customAttribute[j].attributeCode == "image" {
let baseUrl = "http://192.168.1.11/magento2/pub/media/catalog/product"
self.listCategoryImageArray.append(baseUrl + customAttribute[j].value)
print(self.listCategoryImageArray)
}
}
}
self.activityIndicator.stopAnimating()
self.activityIndicator.hidesWhenStopped = true
self.collectionView.delegate = self
self.collectionView.dataSource = self
self.collectionView.reloadData()
self.collectionView.isHidden = false
self.tableView.reloadData()
}
}
} catch {
print(error)
}
}
task.resume()
}
我聲明瞭一個空數組,如下所示,但它在提到的變化行self.listClassModel.append(contentsOf:ModelClass(dict:jsonObj as [String:AnyObject]))中重新調用一個錯誤,錯誤是type [string:anyobject]沒有成員追加var listClassModel:[String:AnyObject] = [:] –
var listClassModel = [ModelClass]()這是數組如何實例化的。你正在上面的代碼中定義一個字典。 – Aks
是兄弟提到 –