gpt4 book ai didi

ios - 如何从解析的服务器和本地存储中删除 TableView 单元格

转载 作者:行者123 更新时间:2023-11-28 08:51:42 24 4
gpt4 key购买 nike

  import UIKit

class MasterTableViewController: UITableViewController, PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate {


// creating array for holding ojects



var noteObjects: NSMutableArray! = NSMutableArray()
var v = 0

override func viewDidLoad() {
super.viewDidLoad()

}

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)



if v == 0 {
self.fetchAllObjectsFromLocalDataStore()
//self.fetchAllObjects()


}
}


// fetching data from local datastrore and from parse

func fetchAllObjectsFromLocalDataStore(){

let query: PFQuery = PFQuery(className: "classname")
query.orderByDescending("createdAt")

query.fromLocalDatastore()

query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

if (error == nil) {




let temp: NSArray = objects as! NSArray

self.noteObjects = temp.mutableCopy() as! NSMutableArray

self.tableView.reloadData()

}else {
print(error!.userInfo)

}
}

}


func fetchAllObjects(){

let query: PFQuery = PFQuery(className: "classname")
query.orderByDescending("createdAt")

query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

if (error == nil) {




PFObject.pinAllInBackground(objects, block: nil )


self.fetchAllObjectsFromLocalDataStore()

// self.tableView.reloadData()


} else {
print(error?.userInfo)

}
}
}





override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}



// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return self.noteObjects.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MasterTableViewCell

let object : PFObject = self.noteObjects.objectAtIndex(indexPath.row) as! PFObject


cell.MasterTitleLabel?.text = object["Title"] as? String
cell.MasterTextLabel.text = object["Fstory"] as? String
cell.MasterTimeLabel.text = object["Time"] as? String
cell.MasterLocationLabel.text = object["Location"] as? String

return cell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("openStory", sender: self)

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

let upcoming: AddNoteTableViewController = segue.destinationViewController as! AddNoteTableViewController

if (segue.identifier == "openStory"){


let indexPath = self.tableView.indexPathForSelectedRow!

let object: PFObject = self.noteObjects.objectAtIndex(indexPath.row) as! PFObject

upcoming.object = object

self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

}


@IBAction func btnReload(sender: AnyObject) {

fetchAllObjects()


}


override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete){


noteObjects.removeObjectAtIndex(indexPath.row)
self.tableView.reloadData()

}
}
}

上面的代码用于检索保存在 Parse 服务器中的对象我也能够检索数据并将其存储在我的本地数据存储中但是我不知道如何在删除时从我的 TableView 中删除选定的单元格从 commitEditing.delete..... 命令它不断向我发送在我本地存储中的对象所以如果有人知道如何删除东西而不是用代码解释我谢谢你有美好的一天:)

最佳答案

在从数组中删除对象之前,您需要从数组中获取对象并对其调用 delete:

let object : PFObject = self.noteObjects.objectAtIndex(indexPath.row) as! PFObject
object.deleteInBackground()

关于ios - 如何从解析的服务器和本地存储中删除 TableView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34021829/

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