UITableView在填充JSON数据时显示虚拟数据

时间:2018-03-11 05:43:13

标签: json swift uitableview custom-cell

我正在尝试使用自定义单元格在UITableView中显示JSON数据,JSON数据在表视图中成功加载。但是,当我第一次运行我的应用程序时,表格视图中的前两行是IB的虚拟单元格,如下所示:

enter image description here

我的自定义单元格如下所示:

enter image description here

主要的是当我向上移动我的表视图时,名为(Workout)的两个虚拟单元格消失,表格视图变为这样,

enter image description here

如何解决这个问题?

我的代码如下所示:

 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
}

1 个答案:

答案 0 :(得分:1)

细胞被重复使用。

如果finallyIs != finalCompare未填充标签,并且如果存在先前使用的值,则会显示此值。您需要添加else子句以为每个标签指定默认值。