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

项目文件

https://jumpshare.com/v/Otai3BBXYwfvyz8jb53k

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

问题:

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

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

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

func getSectionsFromData() -> [Sections] {

    var sectionsArray = [Sections]()

    let animals = Sections(title: "Animals",objects: ["Cats","Dogs","Birds","Lions"])


    sectionsArray.append(animals)

    return sectionsArray


}

我试图做的是:

Alamofire.request(.GET,url).validate().responseJSON { response in
        switch response.result {
        case .Success:
            if let value = response.result.value {
                let json = JSON(value)

                for (_,subJson) in json {
                    for (year,content) in subJson {
                        let title = year
                        let objects = content

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

                    }

                }
            }
        case .Failure(let error):
            print(error)
        }
    }

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

let title = year
let objects = content

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:

{"posts": [
 {
    "Category1": [
        "Post1cat1"
    ],"Category2": [
        "Post1cat2","Post2cat2"
    ]
 }
 ]}

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

编辑:2016年1月29日

所以,我把循环改为:

for (_,subJson) in json {
                for (index,data) in subJson {
                    for (title,objects) in data {
                        sectionsArray.append(Sections(title: title,objects: objects.self.arrayValue.map { $0.string!}))


                    }

                }

            }

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

print("--")
print(title)
print(objects.self.arrayValue.map { $0.string!})
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只是最初定义的部分和上面的单元格.

解决方法

在第二个解析方法(编辑之后),你在最后一个循环中迭代数组,所以你可以在那里创建数组并分别添加每个元素,如例子:
for (title,data) in subJson {
    var elements: [String] = []

    for (_,object) in data {
        if let stringELement = object.rawString() {
            elements.append(stringELement)
        }
    }

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

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

for (_,subJson) in json["posts"] {
    for (title,data) in subJson {
        let optionalCastedobjects = data.arrayObject as? [String]
        let unwrappedobjects = optionalCastedobjects ?? []
        let section = Sections(title: title,objects: unwrappedobjects)

        sectionsArray.append(section)                        
    }
}

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

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

UPDATE

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

func getSectionsFromData(completion: ([Sections]) -> ()) {
    var sectionsArray = [Sections]()

    Alamofire.request(.GET,subJson) in json["posts"] {
                    for (title,data) in subJson {
                        let optionalCastedobjects = data.arrayObject as? [String]
                        let unwrappedobjects = optionalCastedobjects ?? []
                        let section = Sections(title: title,objects: unwrappedobjects)

                        sectionsArray.append(section)
                    }
                }

                completion(sectionsArray)
            }
        case .Failure(let error):
            print(error)
        }
    }
}

和UITableViewController类中的相关部分.

var sections: [Sections] = []

override func viewDidLoad() {
    super.viewDidLoad()

    SectionsData().getSectionsFromData { [weak self](sections: [Sections]) -> () in
        self?.sections = sections
        self?.tableView.reloadData()
    }
}

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...