2016-07-19 2 views
0

여기에 notificationcenter 및 tableview.reloaddata()를 사용하여 사용자 정의 tableview 셀의 삭제 버튼을 클릭 할 때 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 
     } 
    } 

} 

을 어디

이이고이 viewDidAppear 테이블을 다시로드 내 사용자 지정의 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가 포함 된 메인 클래스로 전체 셀을 보낼 수 있습니까? 메서드 처리에서 데이터를 다시 가져올 수 없습니까? Notification() 및 tableview를 다시로드합니까? –

0

시도이다().

0

는 그런 다음 당신은 항상 이전 테이블을 가지고 것입니다, 그렇지 않으면 영향을받는 것입니다 만의 tableview를 tableview를 다시로드하기 전에 데이터 소스를 업데이트해야합니다.

cellforrowAtIndexpathmaindata이 주 데이터 소스라고 생각합니다. 따라서 데이터 소스에서 업데이트하거나 삭제해야하며 데이터를 다시로드해야합니다.

+0

맞아요, 그래서 솔루션은 내가 생각하기에 데이터를 methodhandlingTheNotification() 데이터베이스에서 다시로드하는 것입니다,하지만 당신은 내가 cellforrowAtIndexPath에서 뭔가를해야 할 것 같은데. methodhandlingTheNotification()에서 해당 메소드를 참조하고 tableview를 변경하는 방법은 무엇입니까? maindata는 cellforrowatindexpath 내부의 로컬 변수에 불과합니다. –

관련 문제