[英]How can I display multiple TableViews from a Json File in Swift?
What I want to do is to display my information from a Json file into my TableView on Xcode(Swift). 我想要做的是将我的信息从Json文件显示到Xcode(Swift)上的TableView中。 Here is an example for I want to to: 这是我想要的一个例子:
I want to retrieve my json data from countries, cities and some aditional information from cities. 我想从国家,城市和城市的一些附加信息中检索我的json数据。 In the first screen I want to display the name of the countries in a TableView: 在第一个屏幕中,我想在TableView中显示国家/地区的名称:
Then, when I clic in a cell of one country (USA for example) I want to open a new TableView that contains all the states (or cities) from USA. 然后,当我在一个国家(例如美国)的单元格中clic时,我想打开一个包含来自USA的所有州(或城市)的新TableView。 So I clic on USA and I have the following information in the second screen: 所以我在美国clic,我在第二个屏幕上有以下信息:
And finally when I clic in some state (or city), for example in New York I want to display some aditional information from the state I have selected (New York). 最后,当我在某个州(或城市)陈词滥调时,例如在纽约,我希望从我选择的州(纽约州)中显示一些附加信息。 So in the third screen I have: 所以在第三个屏幕中我有:
"New York is an awesome city...etc" “纽约是一个很棒的城市......等等”
I want to navegate from one tableview to another, so I don´t know how to add the "back" button on the top of the screen, for each tableview. 我想从一个tableview到另一个tableview navegate,所以我不知道如何为每个tableview添加屏幕顶部的“后退”按钮。 The countries and the city i want to display in different cells, so I can clic on them and display the information. 我希望在不同的单元格中显示国家和城市,因此我可以对它们进行陈述并显示信息。
Here is my json example: 这是我的json示例:
[
{
"country": "Brazil",
"city": [
"Sao Paulo",
"Rio de Janeiro"
]
},
{
"country": "Germany",
"city": [
"Berlin",
"Frankfurt"
]
},
{
"country": "France",
"city": [
"Paris",
"Marselle"
]
},
{
"country": "USA",
"city": [
"Florida",
"New York",
"California",
"Nevada"
]
},
{
"country": "Argentina",
"city": [
"Buenos Aires",
"Mendoza",
"La Plata"
]
},
{
"country": "England",
"city": [
"London",
"Manchester",
"Liverpool"
]
}
]
Please can anybody help me to do this? 请有人帮我这样做吗? How can I do it? 我该怎么做? Thanks a lot! 非常感谢!
This is pretty so simple, and most of the work is done by iOS and Xcode, you won't do so much work. 这非常简单,大部分工作都是由iOS和Xcode完成的,你不会做那么多工作。
You need to get some know how in these topics: UITableView
, UINavigationController
and Storyboards
. 您需要了解以下主题: UITableView
, UINavigationController
和Storyboards
。
You can follow this tutorial and you will find that it's a very easy task. 您可以按照本教程进行操作,您会发现这是一项非常简单的任务。
Here's the steps: 这是步骤:
UINavigationController
, so that you get all the navigation stuff for free. 在UINavigationController
嵌入第一个屏幕,这样您就可以免费获得所有导航内容。 prepareForSegue
(described in the tutorial I mentioned above). 使用方法prepareForSegue
(在我上面提到的教程中描述)将选定的国家/地区对象传递到Cities屏幕。 Please read the tutorial first, then you will understand very well the steps I mentioned. 请先阅读教程,然后您将很好地理解我提到的步骤。 It's pretty simple task that take around 1 hour to do. 这项任务非常简单,大约需要1小时。
Your table view data source methods are designed for this use case: 您的表视图数据源方法是针对此用例设计的:
Use a stored NSIndexPath *selected
to track the ui state in didSelectRow 使用NSIndexPath *selected
的存储NSIndexPath *selected
来跟踪didSelectRow中的ui状态
Add an if statment to your data source methods, if (selected)
to cellForRow and numberOfRows if (selected)
到cellForRow和numberOfRows, if (selected)
向数据源方法添加if语句
In the selected == nil
logic branch, return dict.keys.count
for number of rows and return dict.keys[indexPath.row][@"country"]
as the title for your row 在selected == nil
逻辑分支中, return dict.keys.count
行数return dict.keys.count
并返回dict.keys[indexPath.row][@"country"]
作为行的标题
In the selected != nil
branch, return dict[dict.keys[selected.row][@"cities"].keys.count
for number of rows and return dict[dict.keys[selected.row]][@"cities"][indexPath.row]
as the title for your row 在selected != nil
分支中, return dict[dict.keys[selected.row][@"cities"].keys.count
表示行数并返回dict[dict.keys[selected.row]][@"cities"][indexPath.row]
作为行的标题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.