json – 动态生成UITableView单元和头部

前端之家收集整理的这篇文章主要介绍了json – 动态生成UITableView单元和头部前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
项目文件

https://jumpshare.com/v/Otai3BBXYwfvyz8jb53k

(明智地看这些看项目的结构)

问题:

好吧,所以我跟随tutorial创建了一个带标题的UITableView,然后是单元格内容.

代码运行良好,运行良好,现在我想扩展到该教程之外,并使用alamofire和SwiftyJSON动态加载该内容.

在本教程中,使用的代码如下:

  1. func getSectionsFromData() -> [Sections] {
  2.  
  3. var sectionsArray = [Sections]()
  4.  
  5. let animals = Sections(title: "Animals",objects: ["Cats","Dogs","Birds","Lions"])
  6.  
  7.  
  8. sectionsArray.append(animals)
  9.  
  10. return sectionsArray
  11.  
  12.  
  13. }

我试图做的是:

  1. Alamofire.request(.GET,url).validate().responseJSON { response in
  2. switch response.result {
  3. case .Success:
  4. if let value = response.result.value {
  5. let json = JSON(value)
  6.  
  7. for (_,subJson) in json {
  8. for (year,content) in subJson {
  9. let title = year
  10. let objects = content
  11.  
  12. sectionsArray.append(Sections(title: title,objects: objects))
  13.  
  14. }
  15.  
  16. }
  17. }
  18. case .Failure(let error):
  19. print(error)
  20. }
  21. }

如果我打印出他们在控制台中显示的结果 – 所以我知道JSON的获取和循环工作.然后我加入了

  1. let title = year
  2. let objects = content
  3.  
  4. sectionsArray.append(Sections(title: title,objects: objects))

但在这一行:

sectionsArray.append(Sections(title: title,objects: objects))

我收到此错误

cannot convert value of type ‘JSON’ to expected argument type ‘[String]’

这是我正在使用的JSON:

  1. {"posts": [
  2. {
  3. "Category1": [
  4. "Post1cat1"
  5. ],"Category2": [
  6. "Post1cat2","Post2cat2"
  7. ]
  8. }
  9. ]}

有人能帮我吗?我可能会走错方向在这里我想循环JSON并将类别显示标题和表格单元格中的帖子.

编辑:2016年1月29日

所以,我把循环改为:

  1. for (_,subJson) in json {
  2. for (index,data) in subJson {
  3. for (title,objects) in data {
  4. sectionsArray.append(Sections(title: title,objects: objects.self.arrayValue.map { $0.string!}))
  5.  
  6.  
  7. }
  8.  
  9. }
  10.  
  11. }

仍然没有运气.当我添加一些打印件(在:sectionsArray.append下)来测试是否有数据:

  1. print("--")
  2. print(title)
  3. print(objects.self.arrayValue.map { $0.string!})
  4. print(Sections(title: title,objects: objects.self.arrayValue.map { $0.string!}))

我在控制台中得到了这个结果:

Category1

[“Post1cat1”]

Sections(headings: “Category1”,items: [“Post1cat1”])

Category2

[“Post1cat2”,“Post2cat2”]

Sections(headings: “Category2”,items: [“Post1cat2”,“Post2cat2”])

这表明信息存在,但是当我运行应用程序时,仍然没有结果形式他JSON只是最初定义的部分和上面的单元格.

解决方法

在第二个解析方法(编辑之后),你在最后一个循环中迭代数组,所以你可以在那里创建数组并分别添加每个元素,如例子:
  1. for (title,data) in subJson {
  2. var elements: [String] = []
  3.  
  4. for (_,object) in data {
  5. if let stringELement = object.rawString() {
  6. elements.append(stringELement)
  7. }
  8. }
  9.  
  10. sectionsArray.append(Sections(title: title,objects: elements))
  11. }

或者如果您愿意,可以使用来自JSON对象的铸造原始数组,如下例所示:

  1. for (_,subJson) in json["posts"] {
  2. for (title,data) in subJson {
  3. let optionalCastedObjects = data.arrayObject as? [String]
  4. let unwrappedObjects = optionalCastedObjects ?? []
  5. let section = Sections(title: title,objects: unwrappedObjects)
  6.  
  7. sectionsArray.append(section)
  8. }
  9. }

这应该修复提到的编译问题.

但最后请记住,您在同步getSectionsFromData方法中使用异步回调(在您的GET请求中).并且你总是会在回调(clojure)的值附加新数据之前返回数组.这将导致您永远不会显示您以这种方式获取的数据.

UPDATE

要做到这一点,你应该重构你的getSectionsFromData方法,如下所示.

  1. func getSectionsFromData(completion: ([Sections]) -> ()) {
  2. var sectionsArray = [Sections]()
  3.  
  4. Alamofire.request(.GET,subJson) in json["posts"] {
  5. for (title,data) in subJson {
  6. let optionalCastedObjects = data.arrayObject as? [String]
  7. let unwrappedObjects = optionalCastedObjects ?? []
  8. let section = Sections(title: title,objects: unwrappedObjects)
  9.  
  10. sectionsArray.append(section)
  11. }
  12. }
  13.  
  14. completion(sectionsArray)
  15. }
  16. case .Failure(let error):
  17. print(error)
  18. }
  19. }
  20. }

和UITableViewController类中的相关部分.

  1. var sections: [Sections] = []
  2.  
  3. override func viewDidLoad() {
  4. super.viewDidLoad()
  5.  
  6. SectionsData().getSectionsFromData { [weak self](sections: [Sections]) -> () in
  7. self?.sections = sections
  8. self?.tableView.reloadData()
  9. }
  10. }

猜你在找的JavaScript相关文章