Ios 如何使用多维json填充TableView
我需要填充我的TableView,并将单元格自定义为: 点击手机服务打开两个NIVEL,社会服务和其他服务,当我点击其中一个时,他们会自动拨打电话号码 我有一个json响应:Ios 如何使用多维json填充TableView,ios,swift,Ios,Swift,我需要填充我的TableView,并将单元格自定义为: 点击手机服务打开两个NIVEL,社会服务和其他服务,当我点击其中一个时,他们会自动拨打电话号码 我有一个json响应: var phones = { "services": [ { "id": "1", "nome": "Social Service", "numero": "9999-6666"
var phones = {
"services": [
{
"id": "1",
"nome": "Social Service",
"numero": "9999-6666"
},
{
"id": "2",
"nome": "Other Service",
"numero": "9999-7777"
}
],
"directorship": [
{
"id": "3",
"nome": "Directorship 1",
"numero": "9999-8888"
},
{
"id": "4",
"nome": "Directorship 2",
"numero": "9999-9999"
}
]
};
我使用的是Alamofire+SwiftJson,在无量纲json中效果很好,我想我需要更改我的for,但我不知道如何:
我的请求示例:
import UIKit
import Alamofire
import SwiftyJSON
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var texto: [String] = []@ IBOutlet weak
var table: UITableView!
override func viewWillAppear(animated: Bool) {
self.table.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
table.delegate = self
table.dataSource = self
loadPosts()
}
func loadPosts() {
let url = "http://puc.vc/painel/webservice/telefones/"
Alamofire.request(.GET, url)
.responseJSON {
response in
if
let value: AnyObject = response.result.value {
let post = JSON(value)
for (_, subJson) in post {
self.texto.append(subJson.stringValue)
}
}
dispatch_async(dispatch_get_main_queue(), {
self.table!.reloadData()
})
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) - > Int {
return self.texto.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) - > UITableViewCell {
let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel ? .text = self.texto[indexPath.row]
print(self.texto[indexPath.row])
return cell
}
}
我需要这样的东西:
查看图像时,您需要UITableView分区。我将提供一些简单的示例,它很容易出错,但这只是一个提示,所以我希望您对其进行增强
phones
是一个包含两个键“services”和“directorship”的字典,我们将使用这一事实并期望它始终是正确的,在这些键后面是一组字典,每个字典有三个键值对。为了简单起见,我们将有两个字典数组,一个用于服务,一个用于董事职位。首先,我们需要提取它们:
let post = JSON(value)
let services = post["services"] // put it into self.services
let directorship = post["directorship"] // put it into self.directorship
请注意,在现实世界中,它们可能丢失或类型无效(例如,字符串而不是数组),我不考虑这一点
然后我们使用这些数组,如下所示:
// We tell that we have two sections in our table view (services+directorship)
override func numberOfSectionsInTableView(tableView: UITableView)
-> Int {
return 2
}
// Then we return appropriate number of rows for each sections
override func tableView(tableView: UITableView,
numberOfRowsInSection section: Int)
-> Int {
if section == 0
{
return self.services.count
}
else
{
return self.directorship.count
}
}
// Configure cells appropriately
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) - > UITableViewCell {
let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
if section == 0
{
cell.textLabel ? .text = self.services[indexPath.row]["nome"]
}
else
{
cell.textLabel ? .text = self.directorship[indexPath.row]["nome"]
}
return cell
}
// return appropriate section header title
override func tableView(tableView: UITableView,
titleForHeaderInSection section: Int)
-> String {
if section == 0
{
return "Services"
}
else
{
return "Directorship"
}
}
请注意,我希望您不仅仅复制粘贴该代码(它无法工作,我更倾向于在浏览器中编写该代码),而是阅读并分析它
在现实世界中,最好为您的表视图创建模型(即带有phone、name和id的“Service”类),在其他地方提取与web相关的逻辑,使用该web逻辑获取“json”,将其转换为您的模型(同样在“MySuperModel”等单独的类中),并将该模型输入到您的表视图中。快速搜索给了我以下信息:,但我想你可能会在网上找到更多信息。也许你需要两个TableView或UICollectionView?(或部分)@MANIAK_dobrii我不知道哪种方法更好,当我搜索tableview时,我发现最常用的是填充文本和UICollectionView图片,你能举一个例子来解决这个问题吗?正如@MANIAK_dobrii所说,你需要2个部分,你可以将json拆分成2个数组。我不确定你想要实现什么。图片(或伪图形)就足够了。@MANIAK_dobrii只是填充文本,没有图像