gpt4 book ai didi

swift - 在解析数据浏览器中删除用户行

转载 作者:行者123 更新时间:2023-11-28 13:15:01 26 4
gpt4 key购买 nike

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)

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

最佳答案

您应该遵循一些关于如何使用 PFQueryTableViewController 的 Parse 教程,您这样做是错误的。

首先,您在 queryForTable() 中操作 objects 数组。此时查询还没有运行,所以数组仍然是空的,这意味着您的 userObjects 数组也将是空的。如果您想查看查询结果,您应该在 objectsDidLoad 方法中进行。

但我不建议这样做,因为我认为您在这里试图做的是更改从查询返回的对象的排序方式。您应该知道,如果您使用 PFQueryTableViewController,则无法更改查询结果的内容或顺序。将这个数组复制到另一个数组中是行不通的,因为这个新数组不会用于填充 TableView 。

其中一个后果是您不能删除 PFQueryTableViewController 中的项目。如果你想删除项目,你应该实现你自己的 Controller 。在 github 上可能有一些你可以使用的,我鼓励你四处看看,我现在没有想到。

如果您想具体了解您的崩溃,请看这里:您的 TableView 显示了许多对象(存储在 objects 数组中)。当您尝试删除 TableView 中的对象时,您的代码试图从 userObjects 数组中删除该对象,正如我上面所说,该数组是空的。因此崩溃,您不能从空数组中删除对象。

关于swift - 在解析数据浏览器中删除用户行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29318278/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com