简体   繁体   English

如何在Swift中的Json文件中显示多个TableView?

[英]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中显示国家/地区的名称:

  • Brazil 巴西
  • Germany 德国
  • France 法国
  • USA 美国
  • Argentina 阿根廷
  • etc... 等等...

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,我在第二个屏幕上有以下信息:

  • Florida 佛罗里达
  • New York 纽约
  • California 加州
  • Nevada 内华达
  • etc... 等等...

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 . 您需要了解以下主题: UITableViewUINavigationControllerStoryboards

You can follow this tutorial and you will find that it's a very easy task. 您可以按照教程进行操作,您会发现这是一项非常简单的任务。

Here's the steps: 这是步骤:

  1. First of all, you need to design the app UI in a storyboard. 首先,您需要在故事板中设计应用程序UI。 You will create a scene view for the Countries screen, and another one for the Cities, and another one for the Cities Details screen. 您将为“国家/地区”屏幕创建场景视图,为城市创建另一个场景视图,为“城市详细信息”屏幕创建另一个场景视图。
  2. Embed the first screen in UINavigationController , so that you get all the navigation stuff for free. UINavigationController嵌入第一个屏幕,这样您就可以免费获得所有导航内容。
  3. Show the countries data in Countries screen table view. 在States屏幕表视图中显示国家/地区数据。
  4. Make a segue from the Countries table view to Cities screen. 从“国家/地区”表视图到“城市”屏幕创建一个segue。 So that when a country is selected, its cities is shown in the cities screen. 因此,当选择一个国家/地区时,其城市将显示在城市屏幕中。
  5. Pass the selected country object to the Cities screen using the method prepareForSegue (described in the tutorial I mentioned above). 使用方法prepareForSegue (在我上面提到的教程中描述)将选定的国家/地区对象传递到Cities屏幕。
  6. Show the list of cities in the cities screen. 显示城市屏幕中的城市列表。
  7. Do the same when navigating from Cities screen Cities Details screen. 从城市屏幕城市详细信息屏幕导航时执行相同操作。

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.

相关问题 当我有多个TableViews时,如何更改numberOfRowsInSection? - How can I change numberOfRowsInSection when I have multiple TableViews? Swift - 用多维数组中的多个部分填充 TableViews - Swift - Populating TableViews with multiple sections from multidimensional array 如何在 Swift 的两个 Tableviews 中显示一个 JSON 数组 - How to show one JSON array in two Tableviews in Swift 一个UIView Swift中有多个TableViews-如何同时显示两个 - Multiple TableViews in One UIView Swift - How to show both 迅速-如何保存开关,即使我更改TableViews - swift - how to save switch, even I change TableViews 当我使用2个表视图时,如何将数据(托管对象)从一个表视图移动到另一表视图? - How can I move data(managedobject) from one tableview to another tableview when I used 2 tableviews? 我可以在多个 tableViews 中使用单个原型单元格吗? - Can I use a single prototype cell in multiple tableViews? 如何快速读取Documents目录中的json文件? - How can I read a json file in the Documents Directory in swift? Swift 5 - 让 tableview 等到来自 api 调用的数据返回(使用多个 tableviews) - Swift 5 - make tableview wait until data from api call comes back (using multiple tableviews) 如何在Swift中使用两个TableView设计动态布局 - How to design a dynamic layout with two TableViews in Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM