I am making an app in which there is a database for of hotels which contains (h_name, h_address, h_tariff, h_phone and h_latitude and h_longitude to display that hotel on the map).
我正在制作一个应用程序,其中有一个酒店数据库,其中包含(h_name,h_address,h_tariff,h_phone和h_latitude以及h_longitude以在地图上显示该酒店)。
So, here's the deal! I successfully retrieved the hotel names in json encode using PHP web service and they are visible in my tableview cells. But, what I need is, when a user taps on one of the hotels then a new view open (specifically detail view controller) with the hotel details and a map view. And, I am trying to get the Hotel Name, Address, Tariff and Phone details in that detail view controller for that particular hotel, also I want to plot the hotel location in Map View using the latitudes and longitudes.
所以,这是交易!我使用PHP Web服务成功检索了json编码中的酒店名称,它们在我的tableview单元格中可见。但是,我需要的是,当用户点击其中一家酒店然后打开一个新视图(特别是详细视图控制器),其中包含酒店详细信息和地图视图。而且,我想在该特定酒店的详细视图控制器中获取酒店名称,地址,关税和电话详细信息,我还想使用纬度和经度绘制地图视图中的酒店位置。
This is my challenge how to retrieve the hotel details. Here's the ViewControllers Code:
这是我如何检索酒店详细信息的挑战。这是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
}
}
Also, here's the Detail View Controller's code:
此外,这是详细信息视图控制器的代码:
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.
}
}
1 个解决方案
#1
0
It looks like you don't have a Hotel object with which to store the Hotel data, so right now you are only adding the Hotel Name to your data array.
您似乎没有用于存储酒店数据的Hotel对象,因此您现在只需将酒店名称添加到数据阵列中。
Create a Hotel Class file
创建一个酒店类文件
class Hotel {
var name : String?
var latitude : Double?
// etc.
}
then in your data method, add the Hotel objects to your data array
然后在您的数据方法中,将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)
}
}
Then use the hotelarray to populate the new ViewController
然后使用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
}
#1
0
It looks like you don't have a Hotel object with which to store the Hotel data, so right now you are only adding the Hotel Name to your data array.
您似乎没有用于存储酒店数据的Hotel对象,因此您现在只需将酒店名称添加到数据阵列中。
Create a Hotel Class file
创建一个酒店类文件
class Hotel {
var name : String?
var latitude : Double?
// etc.
}
then in your data method, add the Hotel objects to your data array
然后在您的数据方法中,将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)
}
}
Then use the hotelarray to populate the new ViewController
然后使用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
}