Backendless Query的空tableview结果,如何填充到表中

时间:2016-02-10 08:55:43

标签: ios swift backendless

处理与Quora类似的问题和答案应用。从Parse转移到Backendless。

应用程序编译,我在输出中看到数据,但tableview为空。数据不附加数组? (使用Swift 2,Xcode 7.2)

    import UIKit


class QuestionsTableViewController: UITableViewController {

    let backendless = Backendless.sharedInstance()
    var showAllQuestions = [String]()
    //var showAllQuestions = [String:AnyObject] = [Question]() as [String:AnyObject]
    var currentQuestion = [Question]()

    override func viewDidLoad() {
        super.viewDidLoad()

        Types.tryblock({ () -> Void in

            let startTime = NSDate()
            let query = BackendlessDataQuery()
            var allQuestions = self.backendless.persistenceService.of(Question.ofClass()).find(query)
            print("Total Questions in the Backendless starage - \(allQuestions.totalObjects)")

            let bc1 : BackendlessCollection = allQuestions as BackendlessCollection
            for order : Question in bc1.data as! [Question] {

                self.showAllQuestions.appendContentsOf(self.showAllQuestions)
                self.tableView.reloadData()

            }
            var size = allQuestions.getCurrentPage().count
            while size > 0 {
                print("Loaded \(allQuestions) questions in the current page")
                allQuestions = allQuestions.nextPage()
                size = allQuestions.getCurrentPage().count
            }

            print("Total time (ms) - \(1000*NSDate().timeIntervalSinceDate(startTime))")
            },

            catchblock: { (exception) -> Void in
                print("Server reported an error: \(exception as! Fault)")
            }
        )

    }

   override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

       return showAllQuestions.count
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("questionCell",
            forIndexPath: indexPath) as! QuestionsTableViewCell


        cell.userNameLabel.text = showAllQuestions[indexPath.row]
        cell.questionLabel?.text = showAllQuestions[indexPath.row]


        return cell
    }

这输出:

Total Questions in the Backendless starage - 8

Loaded <BackendlessCollection> -> type: BloQuery2.Question, offset: 0, pageSize: 100, totalObjects: 8, data: ( "<BloQuery2.Question: 0x7faab60091f0>", "<BloQuery2.Question: 0x7faab3e1f280>", "<BloQuery2.Question: 0x7faab600aae0>", "<BloQuery2.Question: 0x7faab600bbf0>", "<BloQuery2.Question: 0x7faab600cd80>", "<BloQuery2.Question: 0x7faab600ddf0>", "<BloQuery2.Question: 0x7faab600eeb0>", "<BloQuery2.Question: 0x7faab600ffa0>" ), query: <BackendlessDataQuery> -> properties: (null), whereClause: (null), queryOptions: <QueryOptions> -> { offset = 0; pageSize = 100; related = ( ); } questions in the current page Total time (ms) - 1122.19899892807

2 个答案:

答案 0 :(得分:0)

“self.allQuestions”中有哪些数据? 它不是以字符串形式出现的。它的类型为BloQuery2.Question 请通过任何函数更改类型以从“BloQuery2.Question”获取Value as String。

答案 1 :(得分:0)

请更改

中的代码

覆盖func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath) - &gt;的UITableViewCell

  let blocQuery:BloQuery2 = showAllQuestions[indexPath.row] as BloQuery2
    let onequestion:Question = blockQuery.Question as Question
    cell.userNameLabel.text = onequestion.userName as? String
    cell.questionLabel?.text = onequestion.question as String