[英]Using ObjectMapper - How to JSON result into a TableView swift
I have problem passing my JSON result into a array I use for display in a TableView. 我在将JSON结果传递到用于在TableView中显示的数组时遇到问题。
here my model: 这是我的模型:
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"]
}
}
and here my ViewController 这是我的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
}
}
In the image you can see that my JSON result is OK. 在图像中,您可以看到我的JSON结果正常。 My problem is how to pass this result into de var listRestaurant array: 我的问题是如何将此结果传递给de var listRestaurant数组:
JSON output is OK but I have problem passing this JSON into the var listRestaurant array JSON输出正常,但将此JSON传递到var listRestaurant数组时遇到问题
I will appreciate your help. 感谢您的帮助。 Thanks. 谢谢。
It doesn't look like you are setting the tableview delegate, 您似乎没有设置tableview委托,
in your viewDidLoad, add the following code: 在您的viewDidLoad中,添加以下代码:
tableView.dataSource = self
tableView.delegate = self
This code: 这段代码:
for restaurant in currrently {
if let name = restaurant.name {
restaurant.name = name
you are also setting the restaurant name to the restaurant name, I see no benefit to this line, if you just want to check that the name is set before you add it, do: 您也将餐厅名称设置为餐厅名称,这行没有好处,如果您只想在添加名称之前检查名称是否已设置,请执行以下操作:
if let _ = restaurant.name {
self.listRestaurants.append(restaurant)
}
The mapping you are using, I am not familiar with, so I'm not sure about this one, but the documentation here (if this is what you are using) suggests that you create an object like this: 我不熟悉您正在使用的映射,因此不确定这一点,但是这里的文档(如果您正在使用的是)建议您创建一个这样的对象:
let restaurant = Mapper<Restaurant>().map(restaurantData)
I'm not sure you can just cast it in the way that you are, but, I am not sure. 我不确定您是否可以按照自己的方式进行投射,但是我不确定。
The main reason your tableView is not displaying is because of the delegate functions are not set. 您的tableView无法显示的主要原因是未设置委托函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.