【问题标题】:Json Data not properly reflecting in tableViewJson 数据未正确反映在 tableView 中
【发布时间】:2016-12-15 20:40:50
【问题描述】:

将本地 json 文件加载到表视图和调试器日志中一切正常,但 数据在表视图中重复。我在这里截取了模拟器和日志的屏幕截图 - 可能我认为我在附加数据时遇到了问题。我有 TableViewCell - viewCell 和我的数据类 - admission.swift 和 tableViewController 当然。我正在尝试显示两个数据字段。 tableViewController 的代码 -

var checkins = [attendance]()
override func viewDidLoad() {
    super.viewDidLoad()
    jsonParsingFromFile() 
}
func jsonParsingFromFile()
{
    let path: NSString = NSBundle.mainBundle().pathForResource("jsonFile", ofType: "json")!
    let data : NSData = try! NSData(contentsOfFile: path as String, options: NSDataReadingOptions.DataReadingMapped)
    self.parseJsonData(data)

    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        self.tableView.reloadData()
    })
}

func parseJsonData(data:NSData) -> [attendance]{ 
    do{
        let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
        //parse json data

        let jsonCheckins = jsonResult?["university1"] as! [AnyObject]
        for jsonAttendance in jsonCheckins {

            let checkin = attendance()
            checkin.id = jsonAttendance["id"] as! Int
            checkin.name = jsonAttendance["name"] as! String

            if (creden != checkin.id)
            {

            }
            else
            {
                print(checkin.id)
                print(checkin.name)
                let check = jsonAttendance["attendance"] as! [AnyObject]

                for ch in check {

                checkin.subject = ch["subject"] as! String
                print(checkin.subject)
                checkin.attended = ch["attended"] as! Int
                checkin.done = ch["held"] as! Int
                checkin.atd = (Float(checkin.attended)/Float(checkin.done))*100
                print(checkin.atd , " %")
                checkins.append(checkin)   
            }                
        }
     }
    }
    catch{
        print(error)
    }
    return checkins
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return checkins.count
}

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! viewCell
    cell.subjectLabel.text = checkins[indexPath.row].subject
    cell.attendanceLabel.text = String(checkins[indexPath.row].atd)+" %"    
    return cell
}

【问题讨论】:

  • 你在哪里重新加载表格?
  • 请用代码更新问题,不要发表评论
  • 对不起,伙计。我现在完成了我的编辑,你可以查看它
  • 你是否也覆盖numberOfSections 方法?或只是行数。请同时添加该代码
  • 是的,我也覆盖了它

标签: ios json swift uitableview


【解决方案1】:

错误似乎是因为您仅更新单个对象并将其添加到数组中。因此,最后你在数组中只剩下一种具有相同值的对象

let checkin = attendance()

将其移至循环内

for ch in check {

【讨论】:

  • 实际上我遇到了那个错误,我必须将该类型的array-出席()存储到另一个变量中并插入到那个循环中。但同时保留两者都会产生空白输出。谢谢大佬!
猜你喜欢
  • 1970-01-01
  • 2021-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-23
  • 1970-01-01
相关资源
最近更新 更多