在解析数据浏览器中删除用户行
Delete user row in Parse Data browser
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()
}
}
我在解析中删除用户行时遇到问题。在网络上有很多不同的解决方案,我做了这个 class 但是现在我无法删除我的解析后端中的用户行;当我尝试时收到此错误消息:
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)
对这个问题有什么想法吗?
您应该遵循一些有关如何使用 PFQueryTableViewController
的 Parse 教程,但您的做法是错误的。
首先,你在操作queryForTable()
中的objects
数组。此时查询还没有运行,所以数组还是空的,也就是说你的userObjects
数组也会是空的。如果您想查看查询结果,您应该在 objectsDidLoad
方法中进行。
但我不建议这样做,因为我认为您在这里试图做的是更改从查询返回的对象的排序方式。您应该知道,如果使用 PFQueryTableViewController
,则无法更改查询结果的内容或顺序。将这个数组复制到另一个数组中将不起作用,因为这个新数组将不会用于填充 table 视图。
这样做的众多后果之一是您无法删除 PFQueryTableViewController
中的项目。如果你想删除项目,你应该实现你自己的控制器。 github 上可能有一些你可以使用的,我鼓励你四处看看,我现在没有想到。
如果您想具体了解您的崩溃,请看这里:您的 table 视图显示许多对象(存储在 objects
数组中)。当您尝试删除 table 视图中的对象时,您的代码试图从 userObjects
数组中删除该对象,正如我上面所说,该数组是空的。因此崩溃,您不能从空数组中删除对象。
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()
}
}
我在解析中删除用户行时遇到问题。在网络上有很多不同的解决方案,我做了这个 class 但是现在我无法删除我的解析后端中的用户行;当我尝试时收到此错误消息:
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)
对这个问题有什么想法吗?
您应该遵循一些有关如何使用 PFQueryTableViewController
的 Parse 教程,但您的做法是错误的。
首先,你在操作queryForTable()
中的objects
数组。此时查询还没有运行,所以数组还是空的,也就是说你的userObjects
数组也会是空的。如果您想查看查询结果,您应该在 objectsDidLoad
方法中进行。
但我不建议这样做,因为我认为您在这里试图做的是更改从查询返回的对象的排序方式。您应该知道,如果使用 PFQueryTableViewController
,则无法更改查询结果的内容或顺序。将这个数组复制到另一个数组中将不起作用,因为这个新数组将不会用于填充 table 视图。
这样做的众多后果之一是您无法删除 PFQueryTableViewController
中的项目。如果你想删除项目,你应该实现你自己的控制器。 github 上可能有一些你可以使用的,我鼓励你四处看看,我现在没有想到。
如果您想具体了解您的崩溃,请看这里:您的 table 视图显示许多对象(存储在 objects
数组中)。当您尝试删除 table 视图中的对象时,您的代码试图从 userObjects
数组中删除该对象,正如我上面所说,该数组是空的。因此崩溃,您不能从空数组中删除对象。