将JSON响应附加到tableview -Swift

时间:2015-11-09 23:43:51

标签: ios json swift uitableview cloud-code

我正在调用PFCloud函数,该函数返回用户信用卡的json响应,我想显示一些卡片数据。我有一个我试图追加的阵列而且它没有显示出来。我在viewDidLoad中调用了这个函数。

func loadCards(){

    if let customer = PFUser.currentUser()?.objectForKey("stripe_customer_id") as? String {

        PFCloud.callFunctionInBackground("listCards", withParameters: ["customerId": customer], block: { (success: AnyObject?, error: NSError?) -> Void in

            if error != nil {
                println(error)
            }
            else {
                let responseString = success as? NSArray

                    if let cardToDisplay = responseString!.valueForKey("cardId") as? NSArray{


                        println(cardToDisplay)
                        self.displayCards.addObject(cardToDisplay)


                    }

                self.tableView.reloadData()
            }

        })
    }

}



// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    println(self.displayCards.count)
    return self.displayCards.count

}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell: CardsTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CardsTableViewCell

 self.displayCards[indexPath.row] = cell.card.text!
return cell
}

以下是来自控制台的日志:

0
0
0
0
0
0
(
"card_16v8teBnRIkk4qBepmuz2Mlk",
"card_16ymjoBnRIkk4qBeirtCdYdn",
"card_16ymfzBnRIkk4qBebvP4wwNO",
"card_16ymfzBnRIkk4qBeoRUVccws",
"card_16ymaPBnRIkk4qBeK2ju6ckP",
"card_16ylqyBnRIkk4qBeffUN2bwo",
"card_16ylq3BnRIkk4qBePGngNz8H"
)
1

1 个答案:

答案 0 :(得分:2)

  • 您需要在主线程中重新加载te tableView。当你进入后台提出请求时,你需要这样做:

    dispatch_async(dispatch_get_main_queue()) { () -> Void in
        self.tableView.reloadData()
    }
    

    而不只是self.tableView.reloadData()

  • cardToDisplay似乎是一个数组,因此将self.displayCards.addObject(cardToDisplay)更改为self.displayCards = cardToDisplay

  • self.displayCards[indexPath.row] = cell.card.text!中的cellForRowAtIndexPath行应该反过来..您应该将文本设置为self.displayCards[indexPath.row]。所以它应该是cell.card.text = self.displayCards[indexPath.row]