簡體   English   中英

Swift:從TableView中刪除PFUser進行解析

[英]Swift : Delete PFUser in Parse From a TableView

我正在嘗試從我的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) 

有我的兩節課:

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 = "" 
}

我不明白是什么問題。 您認為我們無法刪除“用戶”表中的用戶嗎?

當我嘗試使用PFUser.currentUser()時,它可以工作,但它只是刪除我當前的用戶,這不是我想要的。

默認情況下,一個用戶無法刪除其他用戶。 您將需要(1)以其他方式登錄,(2)更改訪問權限,或(3)調用CloudCode函數並使用主密鑰在服務器上執行刪除操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM