JSON &Xcode 6 UITableView [英] JSON & Xcode 6 UITableView

查看:23
本文介绍了JSON &Xcode 6 UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个示例,将 Wcf 服务 URL 中的一些 Json 结果绑定到 Xcode 6 中的 UITableView.我尝试将一些教程中的信息结合起来,我发现这些教程侧重于从 Json 创建 NSDictionary 并将其加载到标签或文本框,以及另一个几乎在使用带有 UITableView 的 Json 的 NSArray 的地方,但是 Json 数据源是一个文件路径而不是 URL,它缺少一些 Xcode 初学者需要的关键信息.我是一个不错的面向对象程序员,我在 .NET 方面接受过培训,所以 Obj-C、Cocoa 和 Xcode 对我来说都是非常新的.这和 Xcode 的所有变化,因为其中一些教程让这真的很令人沮丧,所以如果有人能好心地引导我完成它,我将不胜感激.

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.

问题

  1. 我应该使用 TableViewController,还是只使用 ViewController 和 UITableView?为什么?

  1. Should I use a TableViewController, or just a ViewController and a UITableView? And why?

连接到我的服务 URL (dataWithContentsFromURL?) 的最佳方式是什么?我应该如何加载数据以在 UITableView 中使用?NSD字典?NSArray?NSMutableArray?

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?

一个新的项目是用一个ViewController创建的,这个够用吗?我可以只添加一个 UITableView 并完成我的任务吗?我如何命名这个 UITableView?如何设置其数据源?

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?

最终目标

我的 Wcf 服务 URL(如下)返回以下字段,此时我只想将每一行数据加载到我的 UITableView 中,包含 4 个相应字段的列...

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...

服务 URL - http://domain.com/GetEmployees.svc

GetEmployeesMethod URL - http://domain.com/GetEmployees.svc/json/员工

第 1 列 (id) |第 2 列(emp_Name)|第 3 列 (emp_Phone) |第 4 列(emp_Cisco)

Column 1 (id) | Column 2 (emp_Name) | Column 3 (emp_Phone) | Column 4 (emp_Cisco)

预先感谢您提供的任何帮助.到目前为止,我认为我的大部分代码至少接近正确,但是作为 Xcode 的新手,我无法将其中的任何代码绑定到我的视图对象...

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. 差别不大.如果您不想向该控制器添加任何其他视图,那么使用 UITableViewController 会更容易.数据源和委托已经设置,并且您已经拥有一个 tableView 属性.如果你想向这个控制器添加任何其他视图,最好使用 UIViewController(如果你向 UITableViewController 添加任何视图,如果你使用 UITableViewController,它们将成为表视图的一部分,因为控制器的主视图是表查看).

  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).

不,dataWithContentsOfURL 不是一个好方法.你应该使用 NSURLSession.

No, dataWithContentsOfURL is not a good way. You should use NSURLSession.

单个控制器足以显示您的数据.如果你使用 UIViewController,你可以添加一个表视图作为子视图,并随意命名.如果您创建了一个名为 tableView 的属性,您将使用 self.tableView.dataSource = self 设置数据源.

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 没有列,所以如果你想要 4 个列"的数据,你需要创建一个在一行中有 4 个标签的单元格.

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.

这篇关于JSON &Xcode 6 UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆