我正在制作一个应用程序,其中有一个酒店数据库,其中包含(h_name,h_address,h_tariff,h_phone和h_latitude以及h_longitude以在地图上显示该酒店)。
所以,这是交易!我使用PHP Web服务成功检索了json编码中的酒店名称,它们在我的tableview单元格中可见。但是,我需要的是,当用户点击其中一家酒店然后打开一个新视图(特别是详细视图控制器),其中包含酒店详细信息和地图视图。而且,我想在该特定酒店的详细视图控制器中获取酒店名称,地址,关税和电话详细信息,我还想使用纬度和经度绘制地图视图中的酒店位置。
这是我如何检索酒店详细信息的挑战。 这是ViewControllers代码:
import UIKit
class HotelViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var hotelTableView: UITableView!
// Table Data
var hotelArray:[String] = [String]()
var selectedHotel:String?
var hotelName:String?
var hotelAddress:String?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// Set view controller as delegate of tableview
self.hotelTableView.delegate = self
self.hotelTableView.dataSource = self
// Get current hotels
self.retrieveHotels("")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func retrieveHotels(latestHotel:String) {
// Specify the URL of our retrieval web service
let url:NSURL = NSURL(string: "http://localhost/localHotelsSearch.php")!
// Create a NSURLSession task with completion handler
let task:NSURLSessionDataTask = NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { (data:NSData!, response:NSURLResponse!, error:NSError!) -> Void in
// Convert the json data into an array
let dataArray:[AnyObject] = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as [AnyObject]
// Clear the hotel array
self.hotelArray.removeAll(keepCapacity: false)
// Loop through each dictionary in the array
for data in dataArray {
let dictionary:[String:String] = data as [String:String]
// Append it to the hotel array
if dictionary["h_name"] != nil {
self.hotelArray.append(dictionary["h_name"]!)
}
}
dispatch_async(dispatch_get_main_queue()) {
// Refresh the table
self.hotelTableView.reloadData()
}
})
// Run the task
task.resume()
}
// MARK: - Tableview Delegate Methods
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Get a cell
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("hotelCell") as UITableViewCell
// Configure the cell
cell.textLabel?.text = hotelArray[indexPath.row]
// Return the cell
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.hotelArray.count
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Keep track of which article the user selcted
self.selectedHotel = self.hotelArray[indexPath.row]
// trigger the segue to go to the detail view
// self.performSegueWithIdentifier("toHotelDetailViewSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get reference to destination view controller
let detailHVC = segue.destinationViewController as HotelDetailViewController
let myIndexPath = self.hotelTableView.indexPathForSelectedRow()
let row = myIndexPath?.row
// Pass along the selected articles
detailHVC.hotelToDisplay = self.selectedHotel
}
}
此外,这是详细视图控制器的代码:
import UIKit
import MapKit
class HotelDetailViewController: UIViewController {
var hotelToDisplay:String?
@IBOutlet weak var hName_dv: UILabel!
@IBOutlet weak var hAddress_dv: UILabel!
@IBOutlet weak var hTariff_dv: UILabel!
@IBOutlet weak var hContact_dv: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:0)
您似乎没有用于存储酒店数据的酒店对象,因此您现在只需将酒店名称添加到数据阵列。
创建酒店类文件
class Hotel {
var name : String?
var latitude : Double?
// etc.
}
然后在您的数据方法中,将Hotel对象添加到数据数组
// Loop through each dictionary in the array
for data in dataArray {
let dictionary:[String:String] = data as [String:String]
// Append it to the hotel array
if dictionary["h_name"] != nil {
// *You are only adding the hotel NAME to your data array here
// The other data in the dictionary is not being used, Instead capture all of the data you require from your request
var h = Hotel()
h.name = dictionary["h_name"]!
h.latitude = dictionary["h_latitude"]!
self.hotelArray.append(h)
}
}
然后使用hotelarray填充新的ViewController
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get reference to destination view controller
let detailHVC = segue.destinationViewController as HotelDetailViewController
let myIndexPath = self.hotelTableView.indexPathForSelectedRow()
let row = myIndexPath?.row
// Pass along the selected data, you will need to create these variables in your HotelDetailViewController as well in order to set them
let hotelToPass = self.hotelArray[row]
detailHVC.hotelToDisplay = hotelToPass
detailHVC.hotelName = hotelToPass.name
detailHVC.hotelLatitude = hotelToPass.latitude
}