I am fetching data through API and save it into init
. When I show this data into table view then it only shows the first value of dictionary. I want to show all the values of dictionary into my table view.
class:
class FetchedDeparment
{
var depttName: String
var createdDate: String
init(depttName: String, createdDate: String) {
self.depttName = depttName
self.createdDate = createdDate
}
}
Fetching data from API and save it init
.
var fetchedDepttData = [FetchedDeparment]()
fetchedDepttData = []
self.arrDeptt = json["departments"] as! [[String : Any]]
print(self.arrDeptt)
for eachDepartment in json["departments"]!
{
let eachData = eachDepartment as! [String: Any]
let depttName = eachData["name"] as! String
let createdDate = eachData["created_at"] as! String
self.fetchedDepttData.append(FetchedDeparment.init(depttName: depttName, createdDate: createdDate))
}
self.tableView.reloadData()
TableView: This is where I want to show all the data.
func numberOfSections(in tableView: UITableView) -> Int {
return fetchedDepttData.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DepartmentCell", for: indexPath) as! ShowDepttTVCell
cell.lblDepttName.text = fetchedDepttData[indexPath.row].depttName
cell.lblCreatedDate.text = fetchedDepttData[indexPath.row].createdDate
return cell
}
Your code is okay. You have to replace the row with section because you are counted the array in numberOfSection table view data source method.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DepartmentCell", for: indexPath) as! ShowDepttTVCell
cell.lblDepttName.text = fetchedDepttData[indexPath.section].depttName
cell.lblCreatedDate.text = fetchedDepttData[indexPath.section].createdDate
return cell
}