08-02-2010, 05:53 AM
#1 (permalink )
Registered Member
Join Date: Jul 2010
Posts: 150
How delete row from tableview?
Hy guys!
How can I delete a row from a tableview?
I can delete data from the database but not from the tableview.
Thanks.
Ika
08-02-2010, 09:03 AM
#2 (permalink )
Registered Member
Join Date: Jul 2010
Posts: 150
Quote:
Originally Posted by
ikaroweb
Hy guys!
How can I delete a row from a tableview?
I can delete data from the database but not from the tableview.
Thanks.
Ika
i've used this line:
Code:
//Delete the object from the table.
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
but without success.
08-02-2010, 09:23 AM
#3 (permalink )
Registered Member
Join Date: Jul 2010
Location: London, UK
Posts: 35
Quote:
Originally Posted by
ikaroweb
i've used this line:
Code:
//Delete the object from the table.
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
but without success.
And you'll have to update the array containing your table items too.
08-02-2010, 09:38 AM
#4 (permalink )
Registered Member
Join Date: Jul 2010
Posts: 150
i've this error:
Code:
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1261.5/UITableView.m:920
2010-08-02 16:38:24.359 TabBar[829:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'
*** Call stack at first throw:
08-02-2010, 09:51 AM
#5 (permalink )
noob iphone developer
Join Date: Jul 2008
Location: Denmark
Posts: 237
I use an NSMutableArray an populate the tableview with it.
Then i delete from the array and populate the tableview again.
08-02-2010, 10:11 AM
#6 (permalink )
Registered Member
Join Date: Jul 2010
Posts: 150
Quote:
Originally Posted by
Trisect Development
I use an NSMutableArray an populate the tableview with it.
Then i delete from the array and populate the tableview again.
the problem it's delete the cell from tableview.
08-02-2010, 11:32 AM
#7 (permalink )
noob iphone developer
Join Date: Jul 2008
Location: Denmark
Posts: 237
If you delete it from the array with the index you got when you click the tableview, then you can populate the tableview with the new array.
Thats the way I do it.
08-02-2010, 12:30 PM
#8 (permalink )
Registered Member
Join Date: Jun 2009
Posts: 285
remember that the data for the tableview comes from your declared UITableViewDataSource, which is usually your File's Owner as set in IB, if that is where you set up your view.
the table will ask for it's data, including the number or sections and number of rows in each section with the methods from the UITableViewDataSource protocol:
Code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
and
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
these almost always come from an array that you have set up with your data, so:
Code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [myData count];
}
so if you delete a row from the table, but don't delete the row from your array of data, this can cause problems.
as mentioned in the earlier posts, it's best to delete the row from your array, and then reload the table, allowing it to adjust itself.
if you want to delete the row from the table, make sure you delete it from the array, so that next time it is loaded, it will be loaded correctly.
08-02-2010, 01:25 PM
#9 (permalink )
Registered Member
Join Date: Jul 2010
Posts: 150
Quote:
Originally Posted by
mickm
remember that the data for the tableview comes from your declared UITableViewDataSource, which is usually your File's Owner as set in IB, if that is where you set up your view.
the table will ask for it's data, including the number or sections and number of rows in each section with the methods from the UITableViewDataSource protocol:
Code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
and
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
these almost always come from an array that you have set up with your data, so:
Code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [myData count];
}
so if you delete a row from the table, but don't delete the row from your array of data, this can cause problems.
as mentioned in the earlier posts, it's best to delete the row from your array, and then reload the table, allowing it to adjust itself.
if you want to delete the row from the table, make sure you delete it from the array, so that next time it is loaded, it will be loaded correctly.
this is my code:
Quote:
- (void)tableView UITableView *)tableView commitEditingStyle UITableViewCellEditingStyle)ed itingStyle forRowAtIndexPath NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
NSDictionary *itemAtIndex = (NSDictionary *)[dataList objectAtIndex:indexPath.row];
NSString *indice = [itemAtIndex objectForKey:@"id"];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *path = [documentsDir stringByAppendingPathComponent:@"codici.sqlite"];
//NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"codici.sqlite"];
NSString *defaultDBPath = path;
NSLog(@"path: %@", defaultDBPath);
// Open the database from the users filessytem
NSInteger dbOpen;
dbOpen = sqlite3_open([defaultDBPath UTF8String], &database);
NSLog(@"sqlite3_step error: '%s'", sqlite3_errmsg(database));
NSLog(@"dbopen: %@", dbOpen);
if(dbOpen == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
NSString *strSqlStatement = [@"delete from codici where id =" stringByAppendingFormat:@"('%@')",indice];
char *sqlStatement = (char *)[strSqlStatement UTF8String];
NSInteger dbSvar;
char *err;
dbSvar = sqlite3_exec(database, sqlStatement, NULL, NULL, &err);
if(dbSvar == SQLITE_OK) {
NSLog(@"Data removed from database");
//[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
//Delete the object from the table.
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView reloadData];
}
else{
NSLog(@"Error: %@", [NSString stringWithUTF8String:err]);
}
}
sqlite3_close(database);
}
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
}
}
Last edited by ikaroweb; 08-03-2010 at 01:38 AM .
08-03-2010, 06:17 AM
#10 (permalink )
Registered Member
Join Date: Jul 2010
Posts: 150
someone help me?
I tried with a new project and get the same error.
08-04-2010, 04:26 PM
#11 (permalink )
Registered Member
Join Date: Jul 2010
Posts: 150
i've added this line:
Code:
dataList = [[Data alloc] init:defaultDBPath];
this line reload the datalist with the db updated.
before to delete the row from the table and now works fine!
08-07-2010, 11:39 AM
#12 (permalink )
Registered Member
Join Date: Jun 2009
Posts: 285
Quote:
Originally Posted by
ikaroweb
i've added this line:
Code:
dataList = [[Data alloc] init:defaultDBPath];
this line reload the datalist with the db updated.
before to delete the row from the table and now works fine!
glad you got it worked out! sorry for losing track of this, just got really busy...
08-09-2010, 01:08 AM
#13 (permalink )
Registered Member
Join Date: Jul 2010
Location: chennai
Posts: 41
just make sure this method is defined or not with out this method row will not be deleted from table
- (void)setEditing BOOL)editing animated BOOL)animate{
[super setEditing:editing animated:animate];
[self.nametable setEditing:editing animated:YES];
}
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Stats
Members: 175,655
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pungs