Ios 使用Alamofire(Swift 2)从JSON填充tableview单元格

Ios 使用Alamofire(Swift 2)从JSON填充tableview单元格,ios,json,swift,uitableview,alamofire,Ios,Json,Swift,Uitableview,Alamofire,我有以下代码 导入UIKit 进口阿拉莫菲尔 类CheHappyTableViewController:UITableViewController,NSURLConnectionLegate{ var happyHours=[happyHours]() 重写func viewDidLoad(){ super.viewDidLoad() //加载单元元素 loadHappyHourElements() } 重写函数didReceiveMemoryWarning(){ 超级。我收到了记忆警告() /

我有以下代码

导入UIKit
进口阿拉莫菲尔
类CheHappyTableViewController:UITableViewController,NSURLConnectionLegate{
var happyHours=[happyHours]()
重写func viewDidLoad(){
super.viewDidLoad()
//加载单元元素
loadHappyHourElements()
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
func loadHappyHourElements(){
让testhappyhour:HappyHour=HappyHour(标题:“TEST”,图片:“TESST”,描述:“TEST”,后期:“TEST”)
self.happyHours.append(testhappyhours)
让url:String=”https://gist.githubusercontent.com/arianitp/036133ebb5af317a595c/raw/f134ec241ec3126bedd6debe5de371e3e01d225b/happyhours.json"
请求(.GET,url,编码:.JSON).responseJSON
{开关响应中的响应。结果{
case.Success(让JSON):
让response=JSON作为!NSArray
对于响应中的项{//循环遍历数据项
设obj=项为!NSDictionary
设happyhour=happyhour(title:obj[“title”]as!String,image:obj[“image”]as!String,description:obj[“description”]as!String,postedDate:obj[“date”]as!String)
self.happyHours.append(happyhour)
}
案例。失败(let错误):
打印(“请求失败,错误:\(错误)”)
}
}
self.tableView.reloadData()
}
重写func numberOfSectionsInTableView(tableView:UITableView)->Int{
//#警告未完成执行,返回节数
返回1
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
祝你快乐
}
//显示表格中的单元格
重写func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
self.tableView.rowHeight=UIScreen.mainScreen().bounds.size.width
让cellIdentifier=“CheHappyTableViewCellController”
让cell=tableView.dequeueReusableCellWithIdentifier(cellIdentifier,forIndexPath:indexPath)作为!CheHappyTableViewCellController
让happyHour=happyHours[indexPath.row]
cell.lblTitle.text=happyHour.title
//cell.cheHappyImage=happyHour.photo
//配置单元格。。。
返回单元
}
}
即使我在Alamofire请求完成函数中包含了
self.tableView.reloadData()
,表单元格也不会得到更新。我还定义了一个示例对象,其标题和所有属性都设置为“TEST”,这会被加载,但是JSON文件不会填充该表。我可以看到文件被正确下载和读取,但我认为元素不是没有添加到属性变量
happyHours
中,就是没有重新加载元素


我在这里尝试了许多解决方案,但都没有成功。我做错了什么?

您的
self.tableView.reloadData()
行在回调之外,这意味着在加载数据之前,它会立即被调用。试试这个:

func loadHappyHourElements(){

    let testhappyhour:HappyHour = HappyHour(title: "TEST", image: "TESST", description: "TEST", postedDate: "TEST")
    self.happyHours.append(testhappyhour)

    let url:String = "https://gist.githubusercontent.com/arianitp/036133ebb5af317a595c/raw/f134ec241ec3126bedd6debe5de371e3e01d225b/happyhours.json"
    Alamofire.request(.GET, url, encoding:.JSON).responseJSON
        { response in switch response.result {
        case .Success(let JSON):
            let response = JSON as! NSArray
            for item in response { // loop through data items
                let obj = item as! NSDictionary
                let happyhour = HappyHour(title:obj["title"] as! String, image:obj["image"] as! String, description:obj["description"] as! String, postedDate:obj["date"] as! String)
                self.happyHours.append(happyhour)
            }
            self.tableView.reloadData()

        case .Failure(let error):
            print("Request failed with error: \(error)")
            }
    }
}

@RinorBytyçi没问题,很容易犯错误!:)顺便说一句,尝试使用简化JSON响应的处理。