简体   繁体   中英

iOS How to fetch next row parse Swift

I work with DB Back4app it's similar like parse.com

I want to load in tap button, next row from Data Base.

Now I fetch the first row to my label and button when user will press next, the application will load new row from DB.

My code for fetch data:

let query = PFQuery(className: "test")
query.getFirstObjectInBackground{ (objects, error) in
    if error == nil && objects != nil {
        let question: String = objects!["question"] as! String
        let answer1: String = objects!["answer1"] as! String
        let answer2: String = objects!["answer2"] as! String
        let answer3: String = objects!["answer3"] as! String
        let answer5: String = objects!["answer5"] as! String
        self.question = question
        self.answer1 = answer1
        self.answer2 = answer2
        self.answer3 = answer3
        self.answer4 = answer5
        DispatchQueue.main.async {
            self.loadDataToBox()
        }
    } else {
        print("Error")
    }

How can I do this? I can't find information about this. I only found how to load to table view, but I don't use table view.

Example:

例

Ok so with parse you probably dont want to keep calling the server every single time so what you want to do is get your data append it to and array and then just get the information at each index. There are a lot of ways to do this but I'm going to show a simple way to do this. The more you get into parse you can use PFSubclassing and that is a better way to handle your data but for now just do it like this. Create this as a new class

Class QuestionManager{
     var question: String?
     var answerA: String?
     var answerB: String?
     var answerC: String?
     var answerD: String?

}

In the first class do this.

var questions = [QuestionManager]()
var arrayIndex = 0
let query = PFQuery(className: "test")
query.findObjectsInBackground{ (objects, error) in
if error == nil && objects != nil {
    for object in objects as? [PFObject]{
          self.questions.append(QuestionManager(
          question: object!["question"] as! String,
          answerA: object!["answer1"] as! String,
          answerB: object!["answer2"] as! String,
          answerC: object!["answer3"] as! String,
          answerD: object!["answer4"] as! String

          ))
    }
} else {
    print("Error")
}

Then two access it just have the index of the array change

self.label.text = questions[arrayIndex].question

Each time you add one to the array it will return the next set of questions and answers

If you need clarification let me know

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM