问题
I am needing an example of binding some Json results from a Wcf service URL to a UITableView in Xcode 6. I have tried combining information from a couple tutorials I found that focus on creating an NSDictionary from the Json and loading that into a label or text box, and another that was almost spot on in using an NSArray of the Json with a UITableView, however the Json datasource was a file path as opposed to a URL and it was missing some key information that a beginner in Xcode needs. I'm a decent object oriented programmer and my training is in .NET, so Obj-C, Cocoa and Xcode are all extremely new to me. This and all the changes to Xcode since some of these tutorials is making this really frustrating so if anyone could be kind enough to walk me through it I would be greatly appreciative.
QUESTIONS
Should I use a TableViewController, or just a ViewController and a UITableView? And why?
What is the best way to connect to my service URL (dataWithContentsFromURL?) and how should I load the data for use in a UITableView? NSDictionary? NSArray? NSMutableArray?
A new project is created with a single ViewController, is this sufficient to use? Can I just add a UITableView to that and accomplish my tasks? How do I name this UITableView? How do I set its data source?
END GOAL
My Wcf service URL (below) returns the following fields, and I would at this point just like to load each row of data into my UITableView, containing columns for the 4 respective fields...
Service URL - http://domain.com/GetEmployees.svc
GetEmployeesMethod URL - http://domain.com/GetEmployees.svc/json/employees
Column 1 (id) | Column 2 (emp_Name) | Column 3 (emp_Phone) | Column 4 (emp_Cisco)
Thank you in advance for any assistance you can provide. I so far think most of my code is at least close to being correct, but being new to Xcode, I'm lost on binding any of it to my view objects...
回答1:
There's not a lot of difference. If you don't want to add any other views to that controller, then using a UITableViewController would be easier. The data source and delegate are already set, and you already have a tableView property. If you want to add any other views to this controller, it's best to use a UIViewController (if you add any views to a UITableViewController, they become part of the table view if you use a UITableViewController because the main view of the controller is the table view).
No, dataWithContentsOfURL is not a good way. You should use NSURLSession.
A single controller is sufficient to display your data. If you use a UIViewController, you can add a table view as a subview, and name it whatever you want. If you made a property called tableView, you would set the data source with self.tableView.dataSource = self.
UITableViews don't have columns, so you need to create a cell that has 4 labels in a row, if you want 4 "columns" of data.
回答2:
first you have to know how to use UITableView, for this once follow this link Tableview in ios
for using web services in IOS AFNetworkingis good,to know how to use it, once have a look at this
AFNetworking ios,here you find a great explanation for each line
and finally you can use UITabelViewController with custom UITableViewCell with four UILabels for displaying emp details,for this Custom UITableViewCells in ios
来源:https://stackoverflow.com/questions/26052161/json-xcode-6-uitableview