Reputation: 7269
I used fmdb sqlite database wrapper. I'm Confusing with getting selected row label texts. my confusion is, When i swipe and delete the tableView
row then that entire row will be also will delete from database which has that currentCell.userID.text
. so, how to do this? thanks in advance.
code i have try,
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let index = tableView.indexPathForSelectedRow
let currentCell = tableView.cellForRowAtIndexPath(index!) as! DashBoardCell
print(currentCell.userID.text) //I am getting NiL
let deletedID = currentCell.userID.text
if editingStyle == .Delete
{
let tableData = indexPath.row
items.removeAtIndex(tableData)
number.removeAtIndex(tableData)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
let filemgr = NSFileManager.defaultManager()
let dirPaths = filemgr.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
databasePath = dirPaths[0].URLByAppendingPathComponent("visitors.db").path!
let contactDB = FMDatabase(path: self.databasePath as String)
if contactDB.open()
{
let deleteSQL = "SELECT * FROM VISITORLIST WHERE userid = '\(deletedID)'"
let result = contactDB.executeUpdate(deleteSQL, withArgumentsInArray: nil)
if !result
{
print("Failed To Delete Visitor Lists")
print("Error3: ",contactDB.lastErrorMessage())
}
else
{
print("Successfully Deleted")
}
}
else
{
print("Error4: ",contactDB.lastErrorMessage())
}
tableView.reloadData()
}
}
Upvotes: 0
Views: 941
Reputation: 586
let deleteSQL = "SELECT * FROM VISITORLIST WHERE userid = '\(deletedID)'"
Should be :
let deleteSQL = "DELETE FROM VISITORLIST WHERE userid = '\(deletedID)'"
:)
Upvotes: 1
Reputation: 119031
You shouldn't be using let index = tableView.indexPathForSelectedRow
, you should just be using indexPath
. Your current code it deleting the correct object from the table data but the wrong object (or nothing if no row is selected) from the SQLite store.
Upvotes: 1