在Parse Data浏览器中删除用户行

时间:2015-03-28 14:28:22

标签: swift parse-platform row

import UIKit

class ListeUtilisateursPFQueryTableViewController: PFQueryTableViewController, UISearchBarDelegate {

    var images = [NSData]()

    var userObjects: NSMutableArray = NSMutableArray()

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

    // Initialise the PFQueryTable tableview
    override init!(style: UITableViewStyle, className: String!) {
        super.init(style: style, className: className)
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        // Configure the PFQueryTableView
        self.parseClassName = "_User"
        self.textKey = "username"
        self.pullToRefreshEnabled = true
        self.paginationEnabled = false
    }

    // Define the query that will provide the data for the table view
    override func queryForTable() -> PFQuery! {

        userObjects.removeAllObjects()

        var query = PFUser.query()

        for object in objects{
            let user: PFObject = object as PFObject
            self.userObjects.addObject(user)
        }

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

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


        if searchBar.text != "" {
            query.whereKey("searchText", containsString: searchBar.text.lowercaseString)
        }

        query.orderByAscending("username")

        return query
    }

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


    func searchBarTextDidEndEditing(searchBar: UISearchBar) {

        // Dismiss the keyboard
        searchBar.resignFirstResponder()

        // Force reload of table data
        self.loadObjects()
    }

    func searchBarSearchButtonClicked(searchBar: UISearchBar) {

        // Dismiss the keyboard
        searchBar.resignFirstResponder()

        // Force reload of table data
        self.loadObjects()
    }

    func searchBarCancelButtonClicked(searchBar: UISearchBar) {

        // Clear any search criteria
        searchBar.text = ""

        // Dismiss the keyboard
        searchBar.resignFirstResponder()

        // Force reload of table data
        self.loadObjects()
    }

    override func viewDidAppear(animated: Bool) {

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

    }

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        // Get the new view controller using [segue destinationViewController].
        var detailScene = segue.destinationViewController as UtilisateurTableViewCell

        // Pass the selected object to the destination view controller.
        if let indexPath = self.tableView.indexPathForSelectedRow() {
            let row = Int(indexPath.row)
            detailScene.currentObject = objects[row] as? PFObject
        }
    }

    //override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject) -> PFTableViewCell {

        var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as CustomTableViewCell!
        if cell == nil {
            cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
        }

        // Extract values from the PFObject to display in the table cell
        cell.nomUtilisateur.text = object["username"] as String!
        cell.statusUtilisateur.text = object["status"] as String!

        if (cell.statusUtilisateur.text == "Medecin"){
            cell.statusUtilisateur.textColor = UIColor.blueColor()
        }

        if (cell.statusUtilisateur.text == "Client" || cell.statusUtilisateur.text == "Cliente"){
            cell.statusUtilisateur.textColor = UIColor.lightGrayColor()
        }

        if (cell.statusUtilisateur.text == "Secrétariat"){
            cell.statusUtilisateur.textColor = UIColor.brownColor()
        }

        var thumbnail = object["imageFile"] as PFFile
        cell.photoUtilisateur.file = thumbnail
        cell.photoUtilisateur.loadInBackground()

        return cell
    }

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

        var selectedUser:PFObject = self.userObjects.objectAtIndex(indexPath!.row) as PFObject
        selectedUser.deleteInBackground()
        self.userObjects.removeObjectAtIndex(indexPath!.row)
        self.tableView.reloadData()

    }
}

我在解析时删除用户行时遇到问题。在网上有很多不同的解决方案,我创建了这个类但是这一刻我无法删除我的解析后端中的用户行;当我尝试时收到此错误消息:

2015-03-28 15:26:53.209 IOS-EHPAD[4446:610055] -[UIApplication endIgnoringInteractionEvents] called without matching -beginIgnoringInteractionEvents. Ignoring.
2015-03-28 15:26:57.059 IOS-EHPAD[4446:610055] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds for empty array'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010f78ea75 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001112e6bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010f679893 -[__NSArrayM objectAtIndex:] + 227
    3   IOS-EHPAD                           0x000000010df21a04 _TFC9IOS_EHPAD43ListeUtilisateursPFQueryTableViewController9tableViewfS0_FTGSqCSo11UITableView_18commitEditingStyleOSC27UITableViewCellEditingStyle17forRowAtIndexPathGSqCSo11NSIndexPath__T_ + 324
    4   IOS-EHPAD                           0x000000010df21d8f _TToFC9IOS_EHPAD43ListeUtilisateursPFQueryTableViewController9tableViewfS0_FTGSqCSo11UITableView_18commitEditingStyleOSC27UITableViewCellEditingStyle17forRowAtIndexPathGSqCSo11NSIndexPath__T_ + 79
    5   UIKit                               0x000000011011a604 -[UITableView animateDeletionOfRowWithCell:] + 130
    6   UIKit                               0x00000001100faa75 __52-[UITableView _swipeActionButtonsForRowAtIndexPath:]_block_invoke + 72
    7   UIKit                               0x0000000110022a22 -[UIApplication sendAction:to:from:forEvent:] + 75
    8   UIKit                               0x0000000110129e50 -[UIControl _sendActionsForEvents:withEvent:] + 467
    9   UIKit                               0x000000011012921f -[UIControl touchesEnded:withEvent:] + 522
    10  UIKit                               0x0000000110068b68 -[UIWindow _sendTouchesForEvent:] + 735
    11  UIKit                               0x0000000110069493 -[UIWindow sendEvent:] + 683
    12  UIKit                               0x0000000110035fb1 -[UIApplication sendEvent:] + 246
    13  UIKit                               0x0000000110043227 _UIApplicationHandleEventFromQueueEvent + 17700
    14  UIKit                               0x000000011001e23c _UIApplicationHandleEventQueue + 2066
    15  CoreFoundation                      0x000000010f6c3c91 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    16  CoreFoundation                      0x000000010f6b9b5d __CFRunLoopDoSources0 + 269
    17  CoreFoundation                      0x000000010f6b9194 __CFRunLoopRun + 868
    18  CoreFoundation                      0x000000010f6b8bc6 CFRunLoopRunSpecific + 470
    19  GraphicsServices                    0x0000000111e4ca58 GSEventRunModal + 161
    20  UIKit                               0x0000000110021580 UIApplicationMain + 1282
    21  IOS-EHPAD                           0x000000010df0c9de top_level_code + 78
    22  IOS-EHPAD                           0x000000010df0cada main + 42
    23  libdyld.dylib                       0x0000000112454145 start + 1
    24  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

对这个问题有什么想法吗?

1 个答案:

答案 0 :(得分:2)

你应该按照一些关于如何使用PFQueryTableViewController的Parse教程,你会采用错误的方式。

首先,您正在操纵objects中的queryForTable()数组。此时查询尚未运行,因此数组仍为空,这意味着您的userObjects数组也将为空。如果您想查看查询结果,请使用objectsDidLoad方法。

但我建议反对它,因为我认为你在这里尝试做的是改变从查询返回的对象的排序方式。您应该知道,如果使用PFQueryTableViewController,则无法更改查询结果的内容或顺序。在其他数组中复制此数组将不起作用,因为这个新数组不会用于填充表视图。

其中许多后果之一是您无法删除PFQueryTableViewController中的项目。如果要删除项目,则应实现自己的控制器。可能有一些你可以在github上使用,我鼓励你环顾四周,我现在没有想到的。

如果您想要了解崩溃,请在此处:您的表视图显示许多对象(存储在objects数组中)。当您尝试删除表视图中的对象时,您的代码尝试从userObjects数组中删除此对象,如上所述,该数组为空。因此崩溃,您无法从空数组中删除对象。