重新加载集合视图? [英] Reload Collection View?

查看:24
本文介绍了重新加载集合视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have collectionview with cells that displays data from a SQLite database. I also have a button that deletes rows from the SQLite database. I would like to refresh the collectonview and remove the cells that no longer contain the deleted row but my snippet below isn't working, the row gets deleted from the SQLite db but the view isn't refreshed unless I leave that view and come back to it.

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

推荐答案

您可能在删除后还没有更新您的数据数组.所以在你做之前

You probably haven't updated your data array after the deletion. So before you do

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

确保做类似的事情

yourArray = sqldata

其中 yourArray 是用于填充 collectionView 的数组,sqldata 是来自数据库的数据.

Where yourArray is the array that you use to populate your colelctionView and sqldata is the data from the database.

编辑
所以删除以下代码:

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

并用这个 getDataAndReloadCollectionView() 替换它并创建一个函数

and replace it with this getDataAndReloadCollectionView() and create a function

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 时调用这个函数

So basically create a new function that will get the new data, call this function whenever you want to get data and reload your collectionView

这篇关于重新加载集合视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆