ios - swift : Delete PFUser in Parse From a TableView

标签 ios uitableview swift parse-platform pfuser

我正在尝试使用我的 tableView 从解析中删除用户表中的用户。 我在执行此操作时收到此错误消息:

2015-03-29 15:15:00.385 IOS-EHPAD[1717:651792] -[UIApplication endIgnoringInteractionEvents] called without matching -beginIgnoringInteractionEvents. Ignoring.
2015-03-29 15:15:04.221 IOS-EHPAD[1717:651792] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '**User cannot be deleted unless they have been authenticated via logIn or signUp**'
*** First throw call stack:
(0x1854ea530 0x1964740e4 0x1854ea470 0x10015844c 0x100113000 0x10016b668 0x1001149a8 0x100114b94 0x1000d7628 0x1000d7884 0x189efbbd4 0x189fe5094 0x189d2ca14 0x189d15d08 0x189d2c3b0 0x189cebec8 0x189d25ed8 0x189d25578 0x189cf8e60 0x189f9846c 0x189cf73d0 0x1854a2d34 0x1854a1fd8 0x1854a0088 0x1853cd1f4 0x18e7f76fc 0x189d5e10c 0x1000cc8e4 0x1000cc9fc 0x196af2a08)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

这是我的 2 类:

import UIKit

class ListeUtilisateursPFQueryTableViewController: UITableViewController, UISearchBarDelegate {

    var images = [NSData]()

    var users = [""]
    var status = [""]
    var objectId = [""]

    var userObjects: NSMutableArray = NSMutableArray()

    // Table search bar
    @IBOutlet weak var searchBar: UISearchBar!

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

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return userObjects.count
    }

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

        if (editingStyle == UITableViewCellEditingStyle.Delete) {
            let objectToDelete:PFUser = userObjects.objectAtIndex(indexPath.row) as PFUser

            objectToDelete.deleteInBackgroundWithBlock {
                (success: Bool, error: NSError!) -> Void in
                if (success) {
                    // Force a reload of the table - fetching fresh data from Parse platform
                    self.loadData()
                } else {
                    println(error)
                    // There was a problem, check error.description
                }
            }
            self.tableView.reloadData()
        }
    }

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

        let cell:CustomTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomTableViewCell

        cell.nomUtilisateur.text = users[indexPath.row]
        cell.statusUtilisateur.text = objectId[indexPath.row]

        return cell
    }

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

        var cell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!

    }

    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return NO if you do not want the specified item to be editable.
        return true
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        loadData()
    }

    func loadData() {

        userObjects.removeAllObjects()
        users.removeAll(keepCapacity: true)
        status.removeAll(keepCapacity: true)
        objectId.removeAll(keepCapacity: true)

        var findUser: PFQuery = PFUser.query()

        findUser.findObjectsInBackgroundWithBlock{
            (objects:[AnyObject]!, error:NSError!) -> Void in

            for object in objects {

                let user: PFObject = object as PFObject
                self.userObjects.addObject(user)

                self.users.append(object.username)
                self.status.append(object.valueForKey("status") as String)
                self.objectId.append(object.valueForKey("objectId") as String)

            }

            let array:NSArray = self.userObjects.reverseObjectEnumerator().allObjects
            self.userObjects = NSMutableArray(array: array)

            self.tableView.reloadData()
            self.refreshControl?.endRefreshing()
        }

    }

    override func viewDidAppear(animated: Bool) {

        // Refresh the table to ensure any data changes are displayed
        tableView.reloadData()
    }
}

还有我的自定义单元格:

import UIKit
class CustomTableViewCell:UITableViewCell { 
    @IBOutlet weak var nomUtilisateur: UILabel! 
    @IBOutlet weak var statusUtilisateur: UILabel! 
    @IBOutlet weak var photoUtilisateur: PFImageView! 
    var userId = "" 
}

我不明白这是什么问题。您认为我们无法删除 User 表中的用户吗?

当我尝试使用 PFUser.currentUser() 时它可以工作,但它只是删除了我当前的用户,这不是我想要的..

最佳答案

默认情况下,一个用户不能删除另一个用户。您将需要 (1) 以另一个身份登录,(2) 更改访问权限,或 (3) 调用 CloudCode 函数并使用主 key 在服务器上执行删除操作。

关于ios - swift : Delete PFUser in Parse From a TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29329605/

相关文章:

ios - segue 快速获得 twise 调用

ios - 使用 Application Loader 上传时,在包中找不到 .app 包

java - 从另一个类调用 UIWebView 方法

ios - 由于多线程代码导致内存压力而终止

swift - RxSwift。 Observable<Void> 使用什么?

ios - 在多个 View Controller 中使用相同的变量

objective-c - 在 iphone 开发中根据日期明智地对数组对象进行排序?

ios - 台风注入(inject)属性

ios - 以编程方式创建 TableView 时未显示

swift - Self 和 numberOfInSections