I am trying to show JSON data in UITableView using custom cell, the JSON data successfully loads in table view. But, when ever I run my app for the first time, the first two rows in table view are dummy cells form IB as shown below:
My custom cell is shown below:
And the main thing is when ever I swipe up my table view the two dummy cells named (Workout) disappears, and the table view becomes like this,
How do I solve this problem?
My code is shown below:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataEvent.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableViewCalender.dequeueReusableCell(withIdentifier: "EventCustomCell", for: indexPath) as! ScheduleTableViewCell
let totalData = dataEvent[indexPath.row].event_start_date?.components(separatedBy: " ")
let finalStartDate = totalData![0]
let start2018 = finalStartDate.components(separatedBy: "-")
let finalll2018 = start2018[0]
let finall03 = start2018[1]
let finallyIs = "\(finalll2018)-\(finall03)"
if finallyIs == finalCompare{
let finalDate = totalData![1]
cell.startTimeLabel.text = finalDate
cell.titleLabel.text = dataEvent[indexPath.row].event_title
let endData = dataEvent[indexPath.row].event_end_date?.components(separatedBy: " ")
let finalEndDate = endData![1]
cell.endTimeLabel.text = finalEndDate
}
return cell
}
Cells are reused.
If finallyIs != finalCompare
the labels are not populated and if there is a value from a previous use this value is displayed. You need to add an else
clause to assign a default value to each label.