Swift 在分析数据浏览器中删除用户行
我在解析中删除用户行时遇到问题。在web上有很多不同的解决方案,我创建了这个类,但是现在我不能删除解析后端中的用户行;当我尝试时,我收到以下错误消息:Swift 在分析数据浏览器中删除用户行,swift,parse-platform,row,Swift,Parse Platform,Row,我在解析中删除用户行时遇到问题。在web上有很多不同的解决方案,我创建了这个类,但是现在我不能删除解析后端中的用户行;当我尝试时,我收到以下错误消息: import UIKit class ListeUtilisateursPFQueryTableViewController: PFQueryTableViewController, UISearchBarDelegate { var images = [NSData]() var userObjects: NSMutabl
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()
}
}
对这个问题有什么想法吗 您应该遵循一些有关如何使用
PFQueryTableViewController
的解析教程,这是错误的
首先,您正在操作queryForTable()
中的对象
数组。此时查询尚未运行,因此数组仍然为空,这意味着您的userObjects
数组也将为空。如果要查看查询结果,应在objectsDidLoad
方法中执行
但我建议不要这样做,因为我认为您在这里试图做的是更改查询返回的对象的排序方式。您应该知道,如果使用PFQueryTableViewController
,则无法更改查询结果的内容或顺序。在其他数组中复制此数组将不起作用,因为此新数组将不用于填充表视图
这样做的众多后果之一是无法删除PFQueryTableViewController
中的项目。如果要删除项目,则应实现自己的控制器。在github上可能有一些您可以使用的,我鼓励您四处看看,我现在还没有想到
如果您想对崩溃有一个具体的了解,这里就是:您的表视图显示了许多对象(它们存储在
objects
数组中)。当您试图在表视图中删除一个对象时,您的代码试图从userObjects
数组中删除该对象,正如我前面所说,该数组是空的。因此发生了崩溃,您无法从空数组中删除对象。非常感谢您的回答,我更好地理解了我的错误,我正在尝试使用自己的控制器。如果你感兴趣的话,我已经发布了我的最新作品:)我认为你做得对。在删除对象之前,应该先将其从tableview中删除,这样看起来会更好。此外,您不需要首先获取对象,只需删除您拥有的对象即可。我知道objective-C,但不知道swift,所以阅读您的代码对我来说并不容易。如果你有更具体的问题,你应该把它们作为单独的问题发布。好的,谢谢你的回答,我会在睡觉前试试:)明天有更多新闻!我很好奇,因为我面临着同样的问题,你找到解决办法了吗?
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)