2016-07-19 23 views
0

這裏清涼的是我使用刷新tableview中,當我使用通知中心和tableview.reloaddata()點擊我的自定義的tableview細胞刪除按鈕的代碼。我搜索了一堆其他代碼,我認爲我的代碼看起來很好。我不知道爲什麼它不刷新。的tableview不使用notificationcentre和tableview.reloaddata()

這是我的tableview是

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:"NotificationIdentifie‌​r", 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 
     } 
    } 

} 

,這是我的自定義的tableview細胞

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)&notes=\(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("NotificationIdentifie‌​r", object: nil) 


} 

override func awakeFromNib() { 
    super.awakeFromNib() 
} 
override func setSelected(selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated:animated) 
} 
} 
+0

你試過這個,self.tableview.performSelectorOnMainThread(#selector(UITableView.reloadData),withObject:nil,waitUntilDone:false) –

+0

我會試試看,當我回家! –

回答

0

它看起來像你沒有更新,當您單擊「刪除」按鈕values財產。

func methodhandlingTheNotificationEvent(){ 
    // you have to update the `values` array so that it doesn't contain the value you've just removed. 
    tableView.reloadData() 
} 
+0

所以我應該在這種方法中再次從數據庫中獲取數據?哈哈我一直在想,tableview.reloadData()會自動完成所有的工作。而那些其他答案從來沒有提到這個!我會試試看! –

+0

發佈通知時,您可以將'userInfo'中刪除的對象發送出去。但爲此,您需要在單元格中爲其提供參考。 – fiks

+0

我對你在這裏說的話感到困惑。 U意思是在我發佈通知後,我可以將整個單元格發送給包含tablewview的主類。我可以不只是在methodhandlingTheNotification()中重新獲取數據,並重新加載tableview? –

0

嘗試重新加載表viewDidAppear()。

0

你必須重裝tableview那麼只有你的tableview否則會影響你總會有舊錶之前更新您的數據源。

我覺得maindata是主數據源中的cellforrowAtIndexpath。所以你必須更新或刪除你的數據源,然後你應該重新加載數據。

+0

這聽起來很正確的,因此該解決方案,我認爲是在methodhandlingTheNotification()從數據庫中重新加載數據,但你聽起來像我需要做的cellForRowAtIndexPath東西。我將如何能夠從methodhandlingTheNotification()引用該方法並更改tableview,maindata只是cellforrowatindexpath中的局部變量 –

相關問題