重新加载收藏视图?

时间:2016-09-12 18:54:22

标签: ios swift sqlite uicollectionview

我有一个带有显示SQLite数据库数据的单元格的集合视图。我还有一个从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()
    })

1 个答案:

答案 0 :(得分:2)

删除后,您可能尚未更新数据阵列。所以在你做之前

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()
    })
}

所以基本上创建一个新函数来获取新数据,每当你想获取数据并重新加载你的collectionView时调用这个函数