【问题标题】:Load table with titles from JSON从 JSON 加载带有标题的表
【发布时间】:2016-10-04 13:09:32
【问题描述】:

我有一个带有 TableView 的页面,它用一些文本的硬编码 UILabel 填充每个单元格。我希望它用我上网的 JSON 中的 UILabel 填充。

故事板:

代码:

import UIKit

class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!

var objects: NSMutableArray! = NSMutableArray()

override func viewDidLoad(){
super.viewDidLoad()

self.objects.add("iPhone")
self.objects.add("Apple Watch")
self.objects.add("Mac")
self.objects.add("Test")
self.tableView.reloadData()

}

override func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func numberOfSectionsInTableView(_ tableView: UITableView) -> Int{
return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return self.objects.count
}

func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell{

let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell

cell.titleLabel.text = self.objects.object(at: (indexPath as NSIndexPath).row) as? String
//cell.logButton.tag = (indexPath as NSIndexPath).row;
//cell.logButton.addTarget(self, action: #selector(ViewController.logAction(_:)), for: .touchUpInside)
return cell
}

func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath){
self.performSegue(withIdentifier: "showView", sender: self)
}

@IBAction func logAction(_ sender: UIButton) {
let titleString = self.objects[sender.tag] as? String
let firstActivityItem = "\(titleString!)"
let activityViewController : UIActivityViewController = UIActivityViewController(activityItems: [firstActivityItem], applicationActivities: nil)

self.present(activityViewController, animated: true, completion: nil)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if (segue.identifier == "showView"){
            let upcoming: NewViewController = segue.destination as! NewViewController

            let indexPath = self.tableView.indexPathForSelectedRow!

            let titleString = self.objects.object(at: indexPath.row) as? String

            upcoming.titleString = titleString

            self.tableView.deselectRow(at: indexPath, animated: true)
        }

    }

}

在模拟器中打开页面后,TableView 将有四个单元格,其中“标签”更改为添加到 objects 数组中的任何内容,在本例中为 iPhone、Apple Watch、Mac 和 Test .我不想让那些硬编码,而是从 JSON 文件加载项目。

我对 PickerView 做了同样的事情,但我正在努力弄清楚如何使用它。如果有帮助,我的 PickerView 是这样完成的:

Alamofire.request("example.com/file.json").responseJSON{ 响应中

if let JSON = response.result.value as? [String:AnyObject] {


self.mypickerview.delegate = self
self.mypickerview.dataSource = self
let result = JSON.values.flatMap({ String(describing: $0) })
self.pickerData.append(contentsOf: result)
self.pickerData.sort()
self.verbose.text = "Content saved!"
self.mypickerview.reloadAllComponents()
self.mypickerview.delegate = self;
self.verbose.text = "Finished Loading!"

}
}

JSON 文件:

{"One":"Mac","Two":"Apple iPhone","Three":"Test"}

【问题讨论】:

  • 你有没有尝试过?与 PickerView 中所做的基本相同。
  • @Caleb 同样的代码不适用于 NSMutableArray,就像它在以下情况下所做的那样:var pickerData: [String] = [String]() 所以我只是尝试使用一些代码将 NSMutableArray 转换为普通数组,但这不起作用。它显示了一个空白的 TableView

标签: arrays json swift alamofire


【解决方案1】:

1.On viewDidLoad 触发 webService。 2.在Webservice完成处理程序上,检索标签值并分配给对象。 3.重新加载tableview。

【讨论】:

    【解决方案2】:

    您的 JSON 应如下所示:

    { “标题”:[ “苹果电脑”, “苹果手机”, “测试” ] }

    你会做类似cell.titleLabel.text = [[yourJSON valueForKey:@"titles"] objectAtIndex:indexPath.row];的事情

    (这是Obj-C版本,但ofc也可以快速完成)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-23
      • 1970-01-01
      • 2014-09-20
      • 1970-01-01
      • 2018-01-26
      相关资源
      最近更新 更多