1
我是swift的新手,並試圖從http請求中填充表。從Http創建Swift數組GET
該數組在請求內打印得很好,但在外部仍然給出一個空數組並且不填充該表。
我找不出我失蹤的東西。
class affiliates: UIViewController, UITableViewDataSource {
@IBOutlet var tableView: UITableView!
var tableData = [String]()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
// Register custom cell
var nib = UINib(nibName: "vwTblCell3", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "cell3")
// Set the URL where we're making the request
let request = NSURLRequest(URL: NSURL(string: "https://***************")!)
// Perform the request
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{
(response: NSURLResponse?, data: NSData?, error: NSError?)-> Void in
// Get data as string
let str = NSString(data: data!, encoding: NSUTF8StringEncoding)
print(str!)
self.tableData.append(str as! String)
print(self.tableData)
}
);
print(self.tableData)
}
// 2
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableData.count
}
// 3
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell \t {
let cell: TblCell3 = self.tableView.dequeueReusableCellWithIdentifier("cell3") as! TblCell3
cell.lblAffiliate.text = tableData[indexPath.row]
return cell
}
// 4
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("Row \(indexPath.row) selected")
}
// 5
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 70
}
}
任何幫助將非常感激。