2016-05-13 54 views
0

我有一个问题,这是长期处理iv从来没有看到为什么。为什么“显示”Segue更新UITableViewCells而不是reloadData()?

问题

我有一个UItableview并从Firebase中加载数据。当我从ViewController#1继续“显示”到ViewController#2(使用UITableView)时,Firebase数据被加载到单元格中。

每个小区具有UITableViewRowAction“删除”基本上删除火力地堡信息。但是当我按下按钮时,它并没有删除Firebase信息,但没有删除单元格。为了让这个UITableView更新,我必须将show显示回任何其他ViewController并返回到ViewController#2以供它更新。

**此方法仅如果有超过1的UITableViewCell在表

问题#2的更新 - 仅1 UITableViewCell的

现在,如果只有1 UITableViewCell的加载和我“删除”了。 ...它永远不会消失,除非我在ViewController#2中创建一个新的UITableViewCell。

我有什么企图?

self.tableView.endUpdates() 
self.tableView.reloadData() 

㈣硬是插reloadData()的任何地方我可以看看是否会重新载入数据,但它只是不会加载它!

let delete = UITableViewRowAction(style: .Default, title: "Delete", handler: { (action, indexPath) in 
      self.tableView.dataSource?.tableView?(
       self.tableView, 
       commitEditingStyle: .Delete, 
       forRowAtIndexPath: indexPath 
      ) 
      self.tableView.beginUpdates() 
      let uid = NSUserDefaults.standardUserDefaults().valueForKey("uid") as! String // uid 
      DataService.dataService.deleteAudio(uid, project: self.sharedRec.projectName, vocalString: self.sharedRec.audioString) 

      let currURL = self.sharedRec.audioString 
      let url = "\(currURL).caf" 

      //Document Directory 
      let dirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] 
      // Full File to Delete Location 
      let fileURL = dirPath.stringByAppendingPathComponent(url) 
      // This is to print out all directory files 
      let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! 

      do { 
       tableView.beginUpdates() 
       print("DELETED LOCAL RECORD") 

       try NSFileManager.defaultManager().removeItemAtPath(fileURL) 
       print("file deleted \(fileURL)") 
       let directoryContents = try NSFileManager.defaultManager().contentsOfDirectoryAtURL(documentsUrl, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions()) 
       print("PRINT LOCAL DIC\(directoryContents)") // NOT WORKING 
       self.tableView.reloadData() 
      } catch { 
       print("error") 
      } 
      tableView.deleteRowsAtIndexPaths([NSArray arrayWithObject: indexPath], withRowAnimation: .Automatic) // SHOWS ERROR 
      self.tableView.endUpdates() 
      return 
     }) 

error

回答

0
  1. 在功能commitEditingStyle与阵列,数据库等
  2. 递减当前的行数删除数据。
  3. tableView.beginUpdates()
  4. tableView.deleteRowsAtIndexPaths([indexPath],withRowAnimation:。自动)
  5. tableView.endUpdates()
+0

你是说数据被删除之前添加beginUpdates和endUpdates后?另外deleteRowsAtIndexPaths给出错误。iv更新了上面的代码!非常感谢你的帮助! – brkr

+0

请参阅此示例。这是你应该如何做删除行操作。 [链接](http://www.ioscreator.com/tutorials/delete-rows-table-view-ios8-swift) –

相关问题