【问题标题】:Using ObjectMapper - How to JSON result into a TableView swift使用 ObjectMapper - 如何将 JSON 结果快速转换为 TableView
【发布时间】:2016-05-29 16:03:45
【问题描述】:

我在将 JSON 结果传递到用于在 TableView 中显示的数组时遇到问题。

这是我的模型:

class Restaurant: Mappable {

var lat: Float?
var lng: Float?
var address: String?
var name: String?

required init?(_ map: Map) { }

// Mappable
func mapping(map: Map) {
    lat     <- map["venue.location.lat"]
    lng     <- map["venue.location.lng"]
    address <- map["venue.location.address"]
    name    <- map["venue.name"]
}
}

这里是我的 ViewController

class ListViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!

var listRestaurants: [Restaurant] = []

override func viewDidLoad() {
super.viewDidLoad()
retrieveFoursquareList()
tableView.reloadData()
}

func retrieveFoursquareList(){
let foursquareService = FoursquareService()

foursquareService.getFoursquare {
  (let response) in

  if let currrently = response {
    dispatch_async(dispatch_get_main_queue()) {
      print("RESULTADO 2 array: \(currrently)")

      for restaurant in currrently {
        if let name = restaurant.name {
          restaurant.name = name
          print("RESULTADO 3 row: \(restaurant.name)")
          self.listRestaurants.append(restaurant)
          print("RESULTADO 4 array: \(self.listRestaurants)")

        }

      } 
    }
  }
} 
}
}

extension ListViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ->    Int {
return listRestaurants.count
}

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

let cell = tableView.dequeueReusableCellWithIdentifier("RestautantsCell", forIndexPath: indexPath) as! RestaurantCell


let listRestaurant = listRestaurants[indexPath.row]

cell.nameLabel.text = listRestaurant.name
cell.addressLabel.text = listRestaurant.address
return cell
}

}

在图片中你可以看到我的 JSON 结果是好的。我的问题是如何将此结果传递给 de var listRestaurant 数组:

JSON output is OK but I have problem passing this JSON into the var listRestaurant array

感谢您的帮助。谢谢。

【问题讨论】:

    标签: ios json swift uitableview objectmapper


    【解决方案1】:

    您似乎没有设置 tableview 委托,

    在您的 viewDidLoad 中,添加以下代码:

    tableView.dataSource = self
    tableView.delegate = self
    

    这段代码:

    for restaurant in currrently {
        if let name = restaurant.name {
          restaurant.name = name
    

    您还将餐厅名称设置为餐厅名称,我认为这行没有任何好处,如果您只是想在添加之前检查名称是否已设置,请执行以下操作:

    if let _ = restaurant.name {
        self.listRestaurants.append(restaurant)
    }
    

    您正在使用的映射,我不熟悉,所以我不确定这个,但是文档here(如果这是您正在使用的)建议您创建一个这样的对象:

    let restaurant = Mapper<Restaurant>().map(restaurantData)
    

    我不确定您是否可以按照自己的方式进行投射,但是,我不确定。

    tableView 不显示的主要原因是没有设置委托函数。

    【讨论】:

    • 感谢各位代表。我修好了它。但是我在传递数据方面仍然存在问题。我用 let restaurant = Mapper().map(restaurantData) 什么不起作用。
    猜你喜欢
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    相关资源
    最近更新 更多