user3591436 user3591436 - 2 months ago 9
Swift Question

Reload Collection View?

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


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


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

Answer

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

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

Make sure to do something like

yourArray = sqldata

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

Edit
So remove the following code:

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

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

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

Comments