解析对UITableView的响应时遇到问题

时间:2019-11-14 06:09:28

标签: ios json swift uitableview httpresponse

我想解析对UITableView的响应,但是我无法做到这一点。

这是我的UITableView代码。

// MARK: - UITableView Delegate & DataSource
extension ClientLogsDetailVC: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        tableView.backgroundView = nil
        if self.arrExercieLog.count <= 0 {
            tableView.backgroundView = Helper.getPlaceHolderLabel(rect: tableView.frame, message: appDelegate.languageBundle?.localizedString(forKey: "No Data Available!", value: "", table: nil) ?? "No Data Available!")
        }
        return arrExercieLog.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let objSet = arrExercieLog[section]
        return objSet.arrSets.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:ClientExerciseLogCell = self.tblVwExercise.dequeueReusableCell(withIdentifier: "ClientExerciseLogCell", for: indexPath) as! ClientExerciseLogCell

        let objSet = arrExercieLog[indexPath.section]
        let objLaps = objSet.arrSets[indexPath.section].arrLaps[indexPath.row]

        cell.lblExerciseName.text = objLaps.strLapName
        cell.lblExerciseTime.text = objLaps.strLapTimeInHHMMSS

        return cell
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        var header = ClientExerciseLogTableHeader.init(frame: .zero)
        header = Bundle.main.loadNibNamed("ClientExerciseLogTableHeader", owner: self, options: nil)?.first as! ClientExerciseLogTableHeader

        let objSet = arrExercieLog[section]
        header.lblSetName.text      = String.init(format: "Set %d", section + 1)
        header.lblSetTotalTime.text = objSet.strWorkoutTimeInHHMMSS

        return header
    }
}

这是我的模型对象。

class WorkoutObjectNEW: NSObject {
    var nWorkoutID: Int                     = 0
    var strWorkoutName : String             = ""
    var strWorkoutTimeInHHMMSS : String     = ""

    var arrSets: [WorkoutSetsObjectNEW] = []

    class func parseFromDictionary(dict: Dictionary<String, Any>) -> WorkoutObjectNEW {
        let objWorkout = WorkoutObjectNEW()

        if let workoutID = dict["workoutId"] as? Int {
            objWorkout.nWorkoutID = workoutID
        }
        if let workoutName = dict["workoutName"] as? String {
            objWorkout.strWorkoutName = workoutName
        }
        if let workoutDuration = dict["workoutTimeInHHMMSS"] as? String {
            objWorkout.strWorkoutTimeInHHMMSS = workoutDuration
        }

        //SETS
        let arrSets: [Dictionary<String, Any>] = dict["sets"] as! [Dictionary<String, Any>]
        for i in 0..<arrSets.count {
            let dictSet = arrSets[i]

            let objSet = WorkoutSetsObjectNEW()

            if let setDuration = dictSet["setTimeInHHMMSS"] as? String {
                objSet.strSetTimeInHHMMSS = setDuration
            }

            //LAPS
            let arrLaps: [Dictionary<String, Any>] = dictSet["laps"] as! [Dictionary<String, Any>]
            for j in 0..<arrLaps.count {
                let dictLap = arrLaps[j]

                let objLap = LapObject()

                if let lapName = dictLap["lapName"] as? String {
                    objLap.strLapName = lapName
                }
                if let lapDuration = dictLap["lapTimeInHHMMSS"] as? String {
                    objLap.strLapTimeInHHMMSS = lapDuration
                }

                objSet.arrLaps.append(objLap)
            }
            objWorkout.arrSets.append(objSet)
        }

        return objWorkout
    }
}

WorkoutSetsObjectNEW

class WorkoutSetsObjectNEW: NSObject {

    var strSetTimeInHHMMSS: String  = ""

    //LAPs
    var arrLaps: [LapObject] = []

    var strLapName: String          = ""
    var strLapTimeInHHMMSS: String  = ""

}

LapObject

class LapObject: NSObject {
    var strLapName: String          = ""
    var strLapTimeInHHMMSS: String  = ""

    class func parseFromDictionary(dict: Dictionary<String, Any>) -> LapObject {
        let objLap = LapObject()

        if let lapName = dict["lapName"] as? String {
            objLap.strLapName = lapName
        }

        if let lapTime = dict["lapTimeInHHMMSS"] as? String {
            objLap.strLapTimeInHHMMSS = lapTime
        }

        return objLap
    }
}

当前结果是

enter image description here

0 个答案:

没有答案