【问题标题】:Swift: Populate TableView with data that has a specific value in a JSON fileSwift:使用 JSON 文件中具有特定值的数据填充 TableView
【发布时间】:2016-10-27 01:38:19
【问题描述】:

我知道如何使用基于特定键的 JSON 数据填充表视图,但我试图弄清楚如何仅将数据放入表视图中,如果它在键值中有特定值对。

具体来说,我的 JSON 文件包含一个键值对,即: “位置”:“伊利诺伊州芝加哥”, “大陆”:“美国”

我希望我的表格视图显示具有“美国”大陆值的任何条目的位置,并且我不希望它显示具有与其关联的不同大陆的任何位置。

我能得到任何帮助将不胜感激。这是我的 parseJSON 文件中的内容,但目前无法执行我想做的事情。我在 tableview 函数中得到一个超出范围的数组索引

    func parseJSON(){
    do{
        let data = NSData(contentsOfURL: NSURL(string: "https://jsonblob.com/api/jsonBlob/580d0ccce4b0bcac9f837fbe")!)

        let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)

        for anItem in jsonResult as! [Dictionary<String, AnyObject>]{

            let mifiContinent = anItem["continent"] as! String!
            if var mifiContinent = jsonResult["US"]{
                let mifiLocation = anItem["location"] as! String
                let newLocation = Location(location: mifiLocation)
                locationOfMifi.append(newLocation)
            }
        }
    }
    catch let error as NSError{
        print(error.debugDescription)
    }
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if let cell = tableView.dequeueReusableCellWithIdentifier("LocationCell", forIndexPath: indexPath)as? LocationCell{

        let location: Location!

        location = locationOfMifi[indexPath.row]
        cell.configureCell(location)
        return cell

    } else{
        return UITableViewCell()
    }

}

【问题讨论】:

  • 您可能会遇到超出范围的错误,因为您在 cellForRowAtIndexPath 中使用的 locationOfMifiarray 不知何故与您的表数据源不一致。你能也展示你的numberOfRowsInSection 方法吗?
  • 好吧,这听起来像是 CoreData 的完美工作。您必须创建一个数据模型,该模型具有与您的兴趣点相关的所有属性。在你的情况下,那将是大陆。然后,您可以告诉 CoreData 为您获取任何以北美为大陆的东西。使用 CoreData 而不仅仅是一个 Array 将为您的代码创造奇迹。

标签: json swift uitableview


【解决方案1】:

只需对过滤器进行一些更改

let mifiContinent = anItem["continent"] as! String!
if mifiContinent == "US" {
   let mifiLocation = anItem["location"] as! String
   let newLocation = Location(location: mifiLocation)
   locationOfMifi.append(newLocation)
}

【讨论】:

    【解决方案2】:

    您还可以尝试一些 swift-ish 语法来帮助您完成正在做的事情。您必须稍微修改一下您的单元格配置。

    struct Location {
        var location: String?
        var continent: String?
    
        init(_ jsonResult: [String : AnyObject]) {
          self.location = jsonResult["location"] as? String
          self.continent = jsonResult["continent"] as? String
        }
    }
    
    //defined in your class
    var locationOfMifi = [Location]()
    
    //where parsing your json result
    if let jsonResult = jsonResult as? [[String : AnyObject]] {
        locationOfMifi = jsonResult.map({ Location($0) }).filter({ $0.continent == "US" })
    }
    
    //your tableview datasource
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return locationOfMifi.count
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多