删除tableview单元格并刷新视图控制器后刷新tableview [英] Refresh tableview after delete a tableview cell and refresh view controller
问题描述
顺便说一下,我需要快速回答.基本上,我有两个问题需要刷新.如果您认为代码太长而无法查看,这就是我正在尝试做的.一种是在单击 tableview 单元格之一上的删除按钮后刷新 tableview.我试过刷新控制,它没有用,这不是我想要的,但拥有它们会很酷.我的另一个问题是,如何通过单击按钮来刷新视图控制器.这是代码.
I need answer for swift by the way. Basically, I have two questions for refreshing. If you think the code is too long to look at, this is what I'm trying to do. one is to refresh the tableview after clicking on the delete button on one of the tableview cell. I've tried refreshcontrol, it didn't work and it wasn't what I wanted but it would be cool to have them. My other question is, how to refresh a view controller by clicking on of the buttons. And here is the code.
删除按钮在我的自定义单元格类中类 postCell:UITableViewCell{
The delete button is inside of my custom cell class class postCell: UITableViewCell{
@IBOutlet weak var deleteBtn: UIButton!
@IBOutlet weak var postImg: UIImageView!
@IBOutlet weak var category: UILabel!
@IBOutlet weak var location: UILabel!
@IBOutlet weak var title: UILabel!
@IBOutlet weak var price: UILabel!
var prices = String()
var notes = String()
var comments = String()
var locations = String()
var categories = String()
var school = String()
var username = String()
var date = String()
@IBAction func deleteAction(sender: AnyObject) {
let request = NSMutableURLRequest(URL: NSURL(string: "http://www.percyteng.com/orbit/deletePost.php")!)
request.HTTPMethod = "POST"
let postString = "name=\(username)&location=\(locations)&price=\(prices)¬es=\(notes)&school=\(school)&category=\(categories)"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
print("error=\(error)")
return
}
print("response = \(response)")
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
}
task.resume(); NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated:animated)
}
}
我想在另一个类中重新加载数据的tableview:
And the tableview i want to reload data of in another class:
override func viewDidLoad() {
super.viewDidLoad()
username = tempUser.username
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
get{(value) in
self.values = value
for ele in self.values{
if self.username != ele["username"] as! String{
}
}
}
self.tableView.reloadData()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(serviceBoard.methodhandlingTheNotificationEvent), name:"NotificationIdentifier", object: nil)
}
func methodhandlingTheNotificationEvent(){
tableView.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (self.resultSearchController.active && resultSearchController.searchBar.text != "") {
return self.filteredTableData.count
}
else {
return values.count
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as!postCell
let maindata = values[values.count-1-indexPath.row]
if (self.resultSearchController.active && resultSearchController.searchBar.text != "") {
// if (filteredTableData[indexPath.row].rangeOfString("###") == nil){
cell.postImg.image = UIImage(named:"tile_services")
cell.title.text = filteredTableData[indexPath.row]
cell.category.text = "SERVICES"
var price = String()
for i in values{
if (i["title"] as? String)! == filteredTableData[indexPath.row]{
price = (i["price"] as? String)!
cell.categories = "Services"
cell.username = i["username"] as! String
cell.prices = i["price"] as! String
cell.notes = i["notes"] as! String
cell.school = i["school"] as! String
}
}
cell.price.text = price
return cell
}
else {
if maindata["username"] as! String != username && username != "admin"{
cell.deleteBtn.hidden = true
}
else{
cell.categories = "Services"
cell.username = maindata["username"] as! String
cell.prices = maindata["price"] as! String
cell.notes = maindata["notes"] as! String
cell.school = maindata["school"] as! String
}
cell.postImg.image = UIImage(named:"tile_services")
cell.title.text = maindata["title"] as? String
cell.category.text = "SERVICES"
cell.price.text = maindata["price"] as? String
return cell
}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("showPostT") as! showPostT
self.addChildViewController(popOverVC)
popOverVC.view.frame = self.view.frame
self.view.addSubview(popOverVC.view)
popOverVC.didMoveToParentViewController(self)
if (self.resultSearchController.active && resultSearchController.searchBar.text != "") {
for i in values{
if (i["title"] as? String)! == filteredTableData[indexPath.row]{
popOverVC.type.text = "SERVICE"
popOverVC.typeImg.image = UIImage(named:"tile_services")
popOverVC.item.text = "Service: \(i["title"] as! String)"
popOverVC.price.text = "Price: \(i["price"] as! String)"
popOverVC.notes.text = "Notes: \(i["notes"] as! String)"
popOverVC.comments.text = i["comments"] as? String
popOverVC.postUser.text = i["username"] as! String
popOverVC.notesStr = i["notes"] as! String
popOverVC.category = "service"
popOverVC.pricesStr = i["price"] as! String
if username == popOverVC.postUser.text!{
popOverVC.composeBtn.hidden = true
}
}
}
}
else{
let maindata = values[values.count-1-indexPath.row]
popOverVC.type.text = "SERVICE"
popOverVC.typeImg.image = UIImage(named:"tile_services")
popOverVC.item.text = "Service: \(maindata["title"] as! String)"
popOverVC.price.text = "Price: \(maindata["price"] as! String)"
popOverVC.notes.text = "Notes: \(maindata["notes"] as! String)"
popOverVC.comments.text = maindata["comments"] as? String
popOverVC.postUser.text = maindata["username"] as! String
popOverVC.notesStr = maindata["notes"] as! String
popOverVC.category = "service"
popOverVC.pricesStr = maindata["price"] as! String
if username == popOverVC.postUser.text!{
popOverVC.composeBtn.hidden = true
}
}
}
我认为我的第二个问题不需要代码,我只想知道如何刷新视图控制器.我试图关闭当前控制器并重新打开它,但它显然不起作用.
I don't think I need code for my second question, I simply want to know how to refresh a view controller. I tried to close the current controller and reopen it, but it obviously doesn't work.
如果你们能帮助我解决任何问题,我将不胜感激!
I would really appreciate if you guys can help any of the questions!
谢谢!
推荐答案
嗯,首先,如果你想刷新tableview数据你必须通知它,这样它就知道模型是否有任何变化.如果你想重新加载整个表格视图(我强烈建议不要这样做)你可以使用
Well , First of all, if you want to refresh the tableview data you must notify it, so it knows if there are any changes in the model. If you want to reload the whole table view (I strongly advised not to do it) you can use
tableView.reloadData()
如果您想刷新某些行,请使用以下方法:
If you want to refresh certain rows use the following methods:
tableView.reloadRowsAtIndexPaths(paths, withRowAnimation: UITableViewRowAnimation.None)
或者您可以仅将此方法用于删除操作.观察点击按钮事件并向 tableView 发送信号以删除该行.当tableView收到这个信号时,直接通过执行下面的方法即可.
Or you could use this method only for delete action. Observe the tap on button event and send signal to the tableView to delete the row. When the tableView receive this signal, just pass execute the following method.
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
强制重绘自定义视图:view.setNeedsDisplay()
这篇关于删除tableview单元格并刷新视图控制器后刷新tableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!