使用 Alamofire 数据填充表格单元格 [英] Populating Table Cells With Alamofire Data

查看:22
本文介绍了使用 Alamofire 数据填充表格单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一天的大部分时间里,我一直在尝试使用 Alamofire 并使用它来收集一些基于 API 的数据以填充表格.我已经成功地将数据输入到我的 iOS 应用程序中(我可以 println 看到它),但是我一生都无法弄清楚使用我的数据填充正确数量的表行并设置标签的上下文.

For the better part of the day, I've been attempting to play with Alamofire and use it to gather some API-based data to populate a table. I've successfully managed to get the data into my iOS app (I can println to see it), but I cannot for the life of me figure out the context to use my data to populate the correct number of table rows and set a label.

我的网络数据是这样的;

My data from the web is like so;

{
"members": [
    "Bob Dole",
    "Bill Clinton",
    "George Bush",
    "Richard Nixon",
    ]
}

我的 TableViewController 有这样的代码;

My TableViewController has code like so;

...
var group: String?
var memberArr = [String]()
var member: [String] = []
...

override func viewDidLoad() {
    super.viewDidLoad()

    func getData(resultHandler: (data: AnyObject?) -> ()) -> () {
    Alamofire.request(.GET, "http://testurl/api/", parameters: ["groupname": "\(group!)"])
        .responseJSON { (_, _, JSON, _) in
            let json = JSONValue(JSON!)
            let data: AnyObject? = json
            let memberArr:[JSONValue] = json["members"].array!
            for obj in json["members"] {
                let member = obj.string!
            }
            resultHandler(data: data)
        }
    }
...



...
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return memberArr.count
}
....

我的返回 memberArr.count 不起作用

My return memberArr.count does not work

然而,我无法弄清楚如何让我的成员"变量可以在整个控制器中访问,因为我想用它来返回正确的行数或使用成员列表来动态设置每个单元格的标题.

What I cannot figure out, however, is how to get my "member" variable to be accessible throughout the controller, as I'd like to use it to return the proper number of rows or use the list of members to dynamically set the title of each cell.

我知道这是一个新手问题,但我已经通过 StackOverflow 进行了深入研究,似乎没有一个问题适合我的情况.

I know this is a novice question, but I've dug through StackOverflow and none of the questions seem to fit in to my situation.

先谢谢你!

推荐答案

发生的事情是 getData 有一个在后台运行的完成块,你需要告诉 swift 在你读完返回的数据数据后更新表,但您需要在主线程中将此更新发送回:

What is happening is that getData has a completion block that run in the background, you need to tell swift to update the table after you finish reading the returned data data, but you need to send this update back in the main thread:

func getData(resultHandler: (data: AnyObject?) -> ()) -> () {
Alamofire.request(.GET, "http://testurl/api/", parameters: ["groupname": "\(group!)"])
    .responseJSON { (_, _, JSON, _) in
        let json = JSONValue(JSON!)
        let data: AnyObject? = json
        let memberArr:[JSONValue] = json["members"].array!
        for obj in json["members"] {
            let member = obj.string!
        }
        resultHandler(data: data)
        dispatch_async(dispatch_get_main_queue()) {
            self.tableView.reloadData()
        }
    }
}

希望能帮到你!

这篇关于使用 Alamofire 数据填充表格单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆