Can anyone help me how to delete row from database in uitableviewcell using fmdb.
I mean i wrote query about delete in datamanager.m as -(void) deleteTodo; it's working and shown in console as deleted the row. But i wanna delete row in iphone simulator when i press Edit button.
How can i call that -(void) deleteTodo in Datamanger.m class.
My code is.......
-(void) deleteTodo
{
[dataBase executeUpdate:#" delete from todo where pk='8'"];
NSLog(#"Deleted");
}
But how can i use it in rootviewcontroller.m at commitEditingStyle Method. to delete row.
Please Help me....
Than
You should code as follows,
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
// If row is deleted, remove it from the list.
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[dataSource removeDataAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
// Create obj for the class DAtamanager
DataManager *obj= [[DataManager alloc] init];
[obj deleteTodo];
[obj release];
}
}
Related
In my application, I reload my TableView ([tablView reloadData];) after delete row from TableView then canEditRowAtIndexPath Method alway call for (pervious) total number Of Rows.
For Example:
If i have 5 Rows on my TableView, then i delete 1 row from tableView. After deleting, I reload my TableView ([tablView reloadData]) but canEditRowAtIndexPath Method calls 5 time instead of 4 times ??
So i always got Following Error:
Terminating app due to uncaught exception 'NSRangeException', reason: '* **-[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]'
I also tried to reload table after some delay (using NSTimer) but it also not worked for me.
I put some code here:
I apply canEditRowAtIndexPath on specific row which #"status" isEqualToString:#"directory" such like,
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%d", self.listOfSounds.count);
// Return NO if you do not want the specified item to be editable.
if([[[self.listOfSounds objectAtIndex:indexPath.row] objectForKey:#"status"] isEqualToString:#"directory"])
return YES;
else
return NO;
}
Code of delete row:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[self.sql_ deleteSoundFileAudioTableWhereMainID:[[self.listOfSounds objectAtIndex:indexPath.row] objectForKey:#"main_id"]]; /// delete record from DB
[self.listOfSounds removeObjectAtIndex:indexPath.row]; /// delete record from Array
[self updateListofSoundsFile]; /// Custom method
}
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO; // i also tried to return YES;
}
Here updateListofSoundsFile is my custom method code is :
-(void)updateListofSoundsFile
{
if(self.listOfSounds.count > 0)
[self.listOfSounds removeAllObjects];
self.listOfSounds = [self.sql_ getAllDataFromAudioTable]; // get all record from DB
NSLog(#"%d",self.listOfSounds.count);
[self.tblView reloadData];
}
Please Give any suggestion, How can i solve this issue ?
Thanks :)
you need to remove raw from tableview also befor remove item from array and reload data using this line becouse remove item from array but not tableview.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[self.sql_ deleteSoundFileAudioTableWhereMainID:[[self.listOfSounds objectAtIndex:indexPath.row] objectForKey:#"main_id"]]; /// delete record from DB
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.listOfSounds removeObjectAtIndex:indexPath.row]; /// delete record from Array
[self updateListofSoundsFile]; /// Custom method
}
}
I ran into this same problem. The issue was that I deleted the object of the cell but when I used the reloadData method of the tableView, my canEditRowAtIndexPath method was not being called resulting in being able to edit cells that I do not want edited. The true fix was not calling the deleteRowsAtIndexPaths method, but the [tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; method.
Basically here is what was happening:
When I called reloadData:
The raw data was not being removed from the tableView as Nitin said. Therefore this is not the solution.
When I called deleteRowsAtIndexPaths:
iOS detected a discrepancy between the underlying data and the number of cells (because I had already removed the underlying object). The result was a crash which is also not the solution (obviously).
Now for The Fix!
When I called reloadRowsAtIndexPaths:
This caused the tableView to simply reload that single cell AND it got rid of the raw data. This is the solution.
Rather than removing the dataSource object, then trying to remove the cell that is essentially backed by nothing at this point (which causes a crash), simply remove the dataSource object, then reload that indexPath of the tableView
Here is the general format of the method:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[myDataSourceArray removeObjectAtIndex:indexPath.row];
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
This is the first time I ran into the issue with the residual raw data that is not removed by simply calling the reloadData method. This is certainly the most elegant solution that I have seen thus far.
Happy Coding! I hope this helps.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[self.sql_ deleteSoundFileAudioTableWhereMainID:[[self.listOfSounds objectAtIndex:indexPath.row] objectForKey:#"main_id"]]; /// delete record from DB
[self.listOfSounds removeObjectAtIndex:indexPath.row]; /// delete record from Array
[self updateListofSoundsFile]; /// Custom method
}
[self.tblView reloadData];
}
When it comes to me, I analysed as below:
As commented by rmaddy on Nitin Gohel's answer:
You should always remove the data from the data source before updating the table.
I feels this is the ideal way.
If you are going to call reloadData, there is no reason at all to first call deleteRowsAtIndexPath.
This is also looks correct.
I analysed ambiguity, if I write reloadData its crashishing but once I write deleteRowsAtIndexPath it worked well.
Hope someone will emphasise on this issue for its causes etc.
the 'removeObjectAtIndex:indexPath' takes some time and I suspect your [self.tblView reloadData] is being called early. I tried a sample code and found success with [UiTableView beginUpdates] and [UiTableView endUpdates] you may also avoid crash if you put a little delay before the reloading or deleting rows haven't tried it though
[tableTable beginUpdates];
[tableArray removeObjectAtIndex:indexPath.row];
[tableTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableTable endUpdates];
My table doesn't update after removing an item with Edit / Delete or the swipe-delete gesture. If I do Edit / Delete, the Delete button doesn't disappear. It stays there in a pressed/stuck state. When I then click Done, the Delete button goes away. A screenshot of this is attached at the bottom of this post.
My numberOfRowsInSection code is executed when the app is started, but not after the deletion of an item.
I'm using a UITableViewController. The definition in my .h file:
#interface BNVFavoritesTableViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>{
BNVAppDelegate *appDelegate;
IBOutlet UITableView *myTable;
}
#property (retain, nonatomic) IBOutlet UITableView *myTable;
#end
Here's the relevant code from my .m file:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"number of rows:\t%d", [appDelegate.favoritesArray count]);
return [appDelegate.favoritesArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//Get the object from the array.
BNVFavorite *favoriteObj = [appDelegate.favoritesArray objectAtIndex:indexPath.row];
//Set the name.
cell.textLabel.text = favoriteObj.name;
// Set up the cell
return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// If row is deleted, remove it from the list.
if (editingStyle == UITableViewCellEditingStyleDelete){
NSLog(#"before delete\t%d", [appDelegate.favoritesArray count]);
BNVFavorite *selectedObject = [appDelegate.favoritesArray objectAtIndex:indexPath.row];
NSString *selectedName = selectedObject.name;
// delete from sqlite database
[appDelegate removeFavoriteFromDB:selectedName];
// delete from memory array
[appDelegate.favoritesArray removeObjectAtIndex:indexPath.row];
// delete in user interface
[myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
NSLog(#"after delete\t%d", [appDelegate.favoritesArray count]);
}
}
When deleting, I see 'before delete' and 'after delete' messages in my log (as expected), but no 'number of rows' messages.
My datasource and delegate outlets from the Table View are connected to the TableViewController class. I also tried doing this in the ViewDidLoad method, but that didn't make a diferent. Forcing a [myTable reloadData]; at the end of my commitEditingStyle is also not helping.
Finally, here's a screenshot of that 'stuck' delete button.
http://i.stack.imgur.com/ukUu0.png
Clicking it a few times causes an NSRangeException/out of bounds error, indicating that the code from commitEditingStyle is executed.
After hours of searching, I found the problem. Turns out the referencing outlet from the tableview in the storyboard to myTable in my controller class didn't exist anymore. I must have removed it accidentally. I found out about it when I noticed the code started working when I replaced myTable with self.tableView.
You need to add your delete animation between beginUpdates and endUpdates.
[myTable beginUpdates];
[myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[myTable endUpdates];
Just check your connection in connection inspector for your table.
or if it is properly connected then
Reload your table after deleting record
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// If row is deleted, remove it from the list.
if (editingStyle == UITableViewCellEditingStyleDelete){
NSLog(#"before delete\t%d", [appDelegate.favoritesArray count]);
BNVFavorite *selectedObject = [appDelegate.favoritesArray objectAtIndex:indexPath.row];
NSString *selectedName = selectedObject.name;
// delete from sqlite database
[appDelegate removeFavoriteFromDB:selectedName];
// delete from memory array
[appDelegate.favoritesArray removeObjectAtIndex:indexPath.row];
// delete in user interface
[myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
NSLog(#"after delete\t%d", [appDelegate.favoritesArray count]);
[myTable reloadData];
}
}
I have a table view, data were retrieved from database and display in the table rows.
I have a remove button at the top navigation bar from removing table row.
When button is tapped, a red circle delete icon will appear.
After I select delete, it gave me and error call "Program received signal SIGABRT" at the [tableViewDelete rows......]
This is my code.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
Object = [array objectAtIndex:indexPath.row];
[ClassA ClassAMethod:[appDelegate getDBPath] :Object.ID];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
How do I remove a row from the table view?
Does anybody have any ideas or has anybody else achieved anything similar?
Thanks
I assume this line:
[ClassA ClassAMethod:[appDelegate getDBPath] :Object.ID];
deletes your object from database, while table is filled from some array instance - you need to delete an object from that array as well to maintain data integrity (array must be NSMutableArray instance):
Object = [array objectAtIndex:indexPath.row];
[ClassA ClassAMethod:[appDelegate getDBPath] :Object.ID];
// Add the following line
[array removeObjectAtIndex:indexPath:row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
try this code in editing delegate method of tableview:--
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[[self displayedObjects] removeObjectAtIndex:[indexPath row]];
// Animate deletion
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[[self tableView] deleteRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationFade];
}
i hope this can solve your problem
My table is populated with data from a sql DB. When I click on edit I can delete rows and the entry in the DB, but when I click on Done nothing happens.
To implement deleting I use commitEditingStyle:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//Get the object to delete from the array
buddies *buddyObj = [appDelegate.buddiesArray objectAtIndex:indexPath.row];
[appDelegate removeBuddy:buddyObj];
//Delete the object from the table.
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView reloadData];
}
}
I have another table in this app and Edit/Done works fine.
The table with editing working is the first table showed when app starts, the non working table is called clicking on second item of a tabbar.
Any help is appreciated.
Thanks,
Max
Check that delegate is set properly.
I have a problem when trying to delete rows from my UITableView:
The UITableView gets the number of rows from NSMutableArray:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [alertsArray count];
}
I add objects to the NSMutableArray in this way:
- (void)saveButton:(id)sender {
[alertsArray addObject:
[self.tableView reloadData];
}
With this code I allow to delete rows:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
[self.tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[alertsArray removeObjectAtIndex:indexPath.row];
[tableView reloadData];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
From some reason, when trying to delete rows, the application crashes.
Thanks!
You should only do
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
[alertsArray removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
That is, correct your model first, and then call deleteRowsAtIndexPaths:
You will find the appropriate error message in the console window, BTW.
Also, in general no need to use reloadData here, when you didn't change other things as well.