I m building iOS application using swift 4.2 and I have a TableViewController that have a strange behaviour.
This is the code:
import UIKit
class StoreByCategoryViewController: UITableViewController{
@IBOutlet var storeTableView: UITableView!
var categorySelected:CategoryModel?
var listaStore = [StoreModel]()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Elenco Partner"
if((categorySelected) != nil && categorySelected?.id != "0"){
//posso chiamare il WS estraendo tutti i negozi
//per categoria
getStoreByCategory(category : self.categorySelected!);
storeTableView.reloadData()
}else{
getStore();
}
storeTableView.delegate = self
storeTableView.dataSource = self
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return listaStore.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return listaStore.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = storeTableView.dequeueReusableCell(withIdentifier:"customCell") as! StoreTableViewCell
let storeModel = listaStore[indexPath.row]
cell.storeAddress.text = storeModel.address
cell.storePhone.text = storeModel.phoneNumber
cell.storeDescription.text = storeModel.description
if(storeModel.imageUrl != ""){
print(storeModel.imageUrl)
let imageUrl:NSURL = NSURL(string: storeModel.imageUrl!)!
print(storeModel.imageUrl)
DispatchQueue.global(qos: .userInitiated).async {
let imageData:NSData = NSData(contentsOf: imageUrl as URL)!
DispatchQueue.main.async {
let image = UIImage(data: imageData as Data)
cell.storeImage.image = image
cell.storeImage.contentMode = .scaleToFill
}
}
}
// cell.storeImage.layer.cornerRadius = cell.storeImage.frame.height / 2
return cell
}
func getStoreByCategory(category : CategoryModel){
var params = [
"cat_id" : category.id
]
let postUrl = APIRequest(endPoint: "get_store_by_category")
postUrl.sendRequest(parameters: params as! [String : String]) {
responseObject, error in
guard let responseObject = responseObject, error == nil else {
print(error ?? "Unknown error")
return
}
do{
let messageData = try JSONDecoder().decode(ResponseStoreByCategoryModel.self, from: responseObject)
var array = messageData.result
for store in array {
var imageUrl = ""
if(store.image.count > 0){
imageUrl = store.image[0].image
}
let s = StoreModel(id: "",
description: store.firstName,
imageUrl: imageUrl,
address: store.address,
phoneNumber: store.phone)
self.listaStore.append(s)
}
self.storeTableView.reloadData()
}catch{
print("errore durante la decodifica dei dati")
}
}
}
func getStore(){
var params = [
"" : ""
]
let postUrl = APIRequest(endPoint: "get_store")
postUrl.sendRequest(parameters: params as! [String : String]) {
responseObject, error in
let user = CategoryModel(id: "0",
description: "Tutti",
imageUrl: "")
//self.listaCategorie.append(user)
guard let responseObject = responseObject, error == nil else {
print(error ?? "Unknown error")
return
}
do{
let messageData = try JSONDecoder().decode(ResponseStoreModel.self, from: responseObject)
var array = messageData.result
for store in array {
var imageUrl = ""
if(store.image.count > 0){
imageUrl = store.image[0].image
}
let s = StoreModel(id: "",
description: store.firstName,
imageUrl: imageUrl,
address: store.address,
phoneNumber: store.phone)
self.listaStore.append(s)
}
self.storeTableView.reloadData()
}catch{
print("errore durante la decodifica dei dati")
}
}
}
}
Method getStore, getStoreByCategory, return 9 items, but the method
numberOfSection and the method to populate customCell, are called 3 times. So i have a table with 27 items instead of 9 items.
Looks like you want only 1 section with 9 rows in it. If that's the case, you can return 1 from
numberOfSectionsimplementation.The other thing I noticed is - you have a
UITableViewControllerand an@IBOutlet var storeTableView: UITableView!as well. You don't need the outlet, You can useself.tableViewfor the same.