2016-09-12 125 views
0

我有一个显示来自SQLite数据库的数据的单元格的collectionview。我也有一个从SQLite数据库中删除行的按钮。我想刷新collectonview并删除不再包含已删除行的单元格,但下面的代码段不起作用,行将从SQLite数据库中删除,但视图不会刷新,除非我离开该视图并返回到它。重新加载视图?

@IBAction func buttonViewHeartAction(sender: UIButton) { 
    print("buttonViewHeartAction tapped") 
    let face = self.faces[sender.tag] 
    if let imageURL = NSURL(string:face.image) { 
     let path = NSSearchPathForDirectoriesInDomains(
      .DocumentDirectory, .UserDomainMask, true 
      ).first! 
     let db = try! Connection("\(path)/db.sqlite3") 
     let table = Table("coffee_table") 
     let id = Expression<Int64>("id") 
     let name = Expression<String>("name") 
     let url = Expression<String>("url") 
     let alice = table.filter(url == face.directLink) 
     try! db.run(alice.delete()) 
     dispatch_async(dispatch_get_main_queue(),{ 
      self.collectionview.reloadData() 
     }) 
    } 
} 

编辑2:

@IBAction func buttonViewHeartAction(sender: UIButton) { 
    print("buttonViewHeartAction tapped") 
    let face = self.faces[sender.tag] 
    if let imageURL = NSURL(string:face.image) { 
     let path = NSSearchPathForDirectoriesInDomains(
      .DocumentDirectory, .UserDomainMask, true 
      ).first! 
     let db = try! Connection("\(path)/db.sqlite3") 
     let table = Table("coffee_table") 
     let id = Expression<Int64>("id") 
     let name = Expression<String>("name") 
     let url = Expression<String>("url") 
     let alice = table.filter(url == face.directLink) 
     try! db.run(alice.delete()) 
     var newfaces = [face] 
     self.faces = newfaces 
     dispatch_async(dispatch_get_main_queue(),{ 
      self.collectionview.reloadData() 
     }) 
    } 
} 

编辑3:

 let path = NSSearchPathForDirectoriesInDomains(
     .DocumentDirectory, .UserDomainMask, true 
     ).first! 
    let db = try! Connection("\(path)/db.sqlite3") 
    let table = Table("coffee_table") 
    let id = Expression<Int64>("id") 
    let name = Expression<String>("name") 
    let url = Expression<String>("url") 

    var newfaces=[face]() 

    for tables in try! db.prepare(table) { 
     //getting the data at each index 
     let coffeefaceName = (tables[name]) as! String 
     let coffeefaceDirectLink = (tables[url]) as! String 
     let coffeefaceImage = (tables[url]) as! String 

     let newface = face(name:coffeefaceName, 
          directLink: coffeefaceDirectLink, 
          image:coffeefaceImage) 
     newfaces.append(newface) 
    } 
    dispatch_async(dispatch_get_main_queue(),{ 
     self.faces = newfaces 
     self.collectionview.reloadData() 
    }) 
+0

您确定在删除后已正确更新了collectionview数据数组吗? –

+0

我这么认为。我的意思是,如果我离开视图并回到收集视图,删除的数据将不再显示。 – user3591436

回答

1

你可能还没有更新后删除你的数据阵列。所以,你做

dispatch_async(dispatch_get_main_queue(),{ 
    self.collectionview.reloadData() 
}) 

之前,请务必这样做

yourArray = sqldata 

哪里yourArray是您用来填充您colelctionViewsqldata是从数据库中的数据阵列。

编辑
所以删除以下代码:

dispatch_async(dispatch_get_main_queue(),{ 
    self.collectionview.reloadData() 
}) 

以及与此getDataAndReloadCollectionView()替换它,并创建一个功能

func getDataAndReloadCollectionView(){ 
    var newfaces=[face]() 
    for tables in try! db.prepare(table) { 
     //getting the data at each index 
     let coffeefaceName = (tables[name]) as! String 
     let coffeefaceDirectLink = (tables[url]) as! String 
     let coffeefaceImage = (tables[url]) as! String 

     let newface = face(name:coffeefaceName, 
          directLink: coffeefaceDirectLink, 
          image:coffeefaceImage) 
     newfaces.append(newface) 
    } 
    dispatch_async(dispatch_get_main_queue(),{ 
     self.faces = newfaces 
     self.collectionview.reloadData() 
    }) 
} 

所以基本上创建一个新的功能,将让新数据,只要你想获得数据并重新加载你的collection,调用这个函数查看

+0

谢谢Rashwan,我按你的建议做了,但我还没有完成。请参阅我编辑的我按照您的建议添加的两行代码。现在发生的情况是,当我单击删除按钮时,集合视图会刷新,但刷新时只显示已删除的行数据。当我离开视图并回到它时,它不显示已删除的行,只显示那里的行,因为它应该。 – user3591436

+0

当你加载viewController时,你可以设置faces数组,让你看到那一行。 –

+0

是的,当然,请参阅我的编辑3. – user3591436