Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UITableView出现之前,在自定义类之间向viewController传递PFObject_Ios_Swift_Parse Platform - Fatal编程技术网

Ios 在UITableView出现之前,在自定义类之间向viewController传递PFObject

Ios 在UITableView出现之前,在自定义类之间向viewController传递PFObject,ios,swift,parse-platform,Ios,Swift,Parse Platform,我正在用swift 2和Xcode 7构建一个iOS应用程序,我想在模型和控制器之间进行分离。因此,我构建了一个自定义类,该类从Parse查询数据,并加载一个数组[可在ViewController类中找到] 问题是-在ViewController类中找到的UITableView加载到iPhone屏幕的内容是空的,因为数组应该填充PFObject,但直到。输出是:在屏幕上查看tableview之后,数组加载对象,因此它是空的tableview 我需要知道如何使屏幕等待查看,直到UITableVie

我正在用swift 2和Xcode 7构建一个iOS应用程序,我想在模型和控制器之间进行分离。因此,我构建了一个自定义类,该类从Parse查询数据,并加载一个数组[可在
ViewController
类中找到]

问题是-在
ViewController
类中找到的
UITableView
加载到iPhone屏幕的内容是空的,因为数组应该填充
PFObject
,但直到。输出是:在屏幕上查看tableview之后,数组加载对象,因此它是空的tableview

我需要知道如何使屏幕等待查看,直到
UITableView
加载对象

用于从解析查询数据的自定义类

import Foundation
import Parse
import ParseTwitterUtils

class ParseQueryObject {

    init(className:String) {

        let query = PFQuery(className: "Tasks")            
        query.findObjectsInBackgroundWithBlock { (taskLista:[PFObject]?, error:NSError?) -> Void in

            if error == nil {
                taskList = taskLista as [PFObject]!
            } 
            else {
                print(error?.description)
            }
        }
    }        
}
视图控制器:

import UIKit
import Parse
import ParseTwitterUtils

var taskList = [PFObject]()


class TaskListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {

var userAccount:PFUser?

@IBOutlet weak var addNewTaskView: UIView!
@IBOutlet weak var twitterUserNameLabel: UILabel!
@IBOutlet weak var textField: UITextField!

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    UIApplication.sharedApplication().statusBarStyle = .LightContent

    self.tableView.delegate = self
    self.tableView.dataSource = self
    self.textField.delegate = self

    //let name = PFTwitterUtils.twitter()?.screenName
    //self.twitterUserNameLabel?.text = "@ \(name!)"

    self.twitterUserNameLabel.text = userAccount?.username

    self.tableView.delegate = self
    self.tableView.dataSource = self

        ParseQueryObject.init(className: "Tasks")
    }

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)

    UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default

}


func addNewTask() {

    if self.textField.text == "" {

        let alert = UIAlertController(title: "Sorry", message: "You need to add task first.", preferredStyle: UIAlertControllerStyle.Alert)

        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))

        self.presentViewController(alert, animated: true, completion: nil)

    } else {

        let newTask = PFObject(className:"Tasks")

        newTask["description"] = textField.text

        if PFTwitterUtils.twitter()?.screenName == nil {

             newTask["taskUserName"] = userAccount?.username

        } else {

            newTask["taskUserName"] = PFTwitterUtils.twitter()?.screenName

        }


        self.view.userInteractionEnabled = false

        newTask.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in

            if (success) {

                taskList.insert(newTask, atIndex: 0)
                self.tableView.reloadData()
                self.view.userInteractionEnabled = true

                self.textField.text = ""

                self.textField.resignFirstResponder()

                self.tableView.reloadData()


            } else {

                let alert = UIAlertController(title: "Error", message: "there is an error", preferredStyle: UIAlertControllerStyle.Alert)

                alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Cancel, handler: nil))

                self.presentViewController(alert, animated: true, completion: nil)

            }

        }

    }



}

func alert() {

    let alert = UIAlertController(title: "Welcome", message: "Now you can create your own tasks.", preferredStyle: UIAlertControllerStyle.Alert)

    alert.addAction(UIAlertAction(title: "Let's Start it!", style: UIAlertActionStyle.Default, handler: nil))

    self.presentViewController(alert, animated: true, completion: nil)


}


func textFieldShouldReturn(textField: UITextField) -> Bool {

    self.view.endEditing(true)

    addNewTask()

    return false

    }

@IBAction func addNewTaskTapped(sender: AnyObject) {

    addNewTask()

}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    super.view.endEditing(true)

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    super.view.endEditing(true)

}

 func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}



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


    return taskList.count

}

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


    let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

    let taskObj = taskList[indexPath.row]

    let user = taskObj["User"] as? PFUser

    do {

        try user?.fetchIfNeeded()

    } catch{}


    cell.textLabel?.text = taskObj.objectForKey("description") as? String

    return cell

}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    let delete = taskList[indexPath.row]

    delete.deleteInBackground()

    taskList.removeAtIndex(indexPath.row)

    self.tableView.reloadData()

    }
导入UIKit
导入解析
导入ParseTwitterUtils
var taskList=[PFObject]()
类TaskListViewController:UIViewController、UITableViewDataSource、UITableViewDelegate、UITextFieldDelegate{
var用户帐户:PFUser?
@IBOutlet弱var addNewTaskView:UIView!
@IBMOutlet弱var twitterUserNameLabel:UILabel!
@IBOutlet弱var textField:UITextField!
@IBVAR表格视图:UITableView!
重写func viewDidLoad(){
super.viewDidLoad()
UIApplication.sharedApplication().statusBarStyle=.LightContent
self.tableView.delegate=self
self.tableView.dataSource=self
self.textField.delegate=self
//让name=PFTwitterUtils.twitter()?.screenName
//self.twitterUserNameLabel?.text=“@\(名称!)”
self.twitterUserNameLabel.text=userAccount?.username
self.tableView.delegate=self
self.tableView.dataSource=self
ParseQueryObject.init(类名:“任务”)
}
覆盖功能视图将消失(动画:Bool){
超级。视图将消失(动画)
UIApplication.sharedApplication().statusBarStyle=UIStatusBarStyle.Default
}
func addNewTask(){
如果self.textField.text==“”{
let alert=UIAlertController(标题:“对不起”,消息:“您需要先添加任务”,首选样式:UIAlertControllerStyle.alert)
addAction(UIAlertAction(标题:“确定”,样式:UIAlertActionStyle.Default,处理程序:nil))
self.presentViewController(警报、动画:true、完成:nil)
}否则{
让newTask=PFObject(类名:“任务”)
newTask[“description”]=textField.text
如果PFTwitterUtils.twitter()?.screenName==nil{
newTask[“taskUserName”]=用户帐户?.username
}否则{
newTask[“taskUserName”]=PFTwitterUtils.twitter()?.screenName
}
self.view.userInteractionEnabled=false
newTask.SaveInBackgroundithBlock{(成功:Bool,错误:NSError?->中的Void
如果(成功){
任务列表.插入(新任务,索引:0)
self.tableView.reloadData()
self.view.userInteractionEnabled=true
self.textField.text=“”
self.textField.resignFirstResponder()辞职
self.tableView.reloadData()
}否则{
让alert=UIAlertController(标题:“错误”,消息:“有错误”,首选样式:UIAlertControllerStyle.alert)
addAction(UIAlertAction(标题:“确定”,样式:UIAlertActionStyle.Cancel,处理程序:nil))
self.presentViewController(警报、动画:true、完成:nil)
}
}
}
}
func警报(){
让alert=UIAlertController(标题:“欢迎”,消息:“现在您可以创建自己的任务了。”,首选样式:UIAlertControllerStyle.alert)
addAction(UIAlertAction(标题:“让我们开始吧!”,样式:UIAlertActionStyle.Default,处理程序:nil))
self.presentViewController(警报、动画:true、完成:nil)
}
func textField应返回(textField:UITextField)->Bool{
self.view.endEditing(true)
addNewTask()
返回错误
}
@iAction func addNewTaskTapped(发送方:AnyObject){
addNewTask()
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
super.view.endEditing(true)
}
func tableView(tableView:UITableView,didSelectRowAtIndexPath:nsindepath){
super.view.endEditing(true)
}
func numberOfSectionsInTableView(tableView:UITableView)->Int{
返回1
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
return taskList.count
}
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
let cell=UITableViewCell(样式:UITableViewCellStyle.Default,reuseIdentifier:“单元格”)
让taskObj=taskList[indexPath.row]
将user=taskObj[“user”]设为PFUser
做{
请尝试用户?.fetchIfNeeded()
}捕获{}
cell.textlab?.text=taskObj.objectForKey(“说明”)为?字符串
返回单元
}
func tableView(tableView:UITableView,CommittedItingStyle编辑样式:UITableViewCellEditingStyle,forRowAtIndexPath indexPath:NSIndexPath){
让delete=taskList[indexPath.row]
delete.deleteInBackground()
taskList.removatendex(indexPath.row)
self.tableView.reloadData()
}

}这是一个很好的开始。关键思想是要认识到异步运行的函数通常需要告诉调用方它们已经完成了。视图控制器需要知道,以便可以重新加载tableView。与parse的sdk一样,您的查询
init
方法应该用回调声明

func init(className:String, completion: (results:[PFObject]?, error:NSError?) -> Void) {
    // your code as you have it, then, when inside the find...
    query.findObjectsInBackgroundWithBlock { // ...
        if error == nil {
            taskList = taskLista as [PFObject]!
            completion(results:taskLista, error:nil)
        }
        // ...
}
现在,视图控制器可以知道何时完成查询,因此,何时更新UI

self.tableView.dataSource = self
ParseQueryObject.init(className: "Tasks" completion: { (taskLista:[PFObject]?, error:NSError?) -> Void in
    self.tableView.reloadData()
})

为了更好地说明代码的责任,
init
应该始终放置初始化代码,而不是进行调用,并且有一个用于指示加载数据完成的完成。我想这就是初始化过程的结束。相反,您可以使用另一种方法,如so