我集成了alamofire,但在服务器通信中使用数据时遇到了一个问题。
在我讲述我的问题之前,我将展示我的代码:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var MainViewTable: UITableView!
@IBOutlet weak var SidesOpen: UIBarButtonItem!
@IBOutlet weak var GroupButton: UIButton!
var apps:[sample] = [sample]()
override func viewDidLoad() {
super.viewDidLoad()
SidesOpen.target = self.revealViewController()
SidesOpen.action = Selector("revealToggle:")
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
self.setUpSample()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setUpSample() {
Alamofire.request(.GET, "http://{url}").responseJSON{ (request, response, data, error) in
var json = JSON(data!)
for var index = 0; index < json["store"].count; ++index{
var marketinfo = json["store"][index]["name"].stringValue
var imageUrl = json["store"][index]["img"].stringValue
let sample_menu = sample(marketInfo: marketinfo, imageName: imageUrl, button: "")
self.apps.append(sample_menu)
}
}
print(self.apps)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return apps.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
print(setUpSample())
let cell: TblCell = tableView.dequeueReusableCellWithIdentifier("marketCell") as! TblCell
let sample = apps[indexPath.row]
cell.setCell(sample.marketInfo , imageName: sample.imageName, Coupon: "s/t.jpeg")
return cell
}
}
在setUpSample函数中,我遇到了一个我不知道传递或取出JSON数据的问题。在函数中,我尝试打印结果,但是结果是空的。