Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-02-2010, 05:53 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 150
ikaroweb is on a distinguished road
Default 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
ikaroweb is offline   Reply With Quote
Old 08-02-2010, 09:03 AM   #2 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 150
ikaroweb is on a distinguished road
Default

Quote:
Originally Posted by ikaroweb View Post
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.
ikaroweb is offline   Reply With Quote
Old 08-02-2010, 09:23 AM   #3 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: London, UK
Posts: 35
DaveF is on a distinguished road
Default

Quote:
Originally Posted by ikaroweb View Post
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.
DaveF is offline   Reply With Quote
Old 08-02-2010, 09:38 AM   #4 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 150
ikaroweb is on a distinguished road
Default

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:
ikaroweb is offline   Reply With Quote
Old 08-02-2010, 09:51 AM   #5 (permalink)
noob iphone developer
 
Join Date: Jul 2008
Location: Denmark
Posts: 237
Trisect Development is on a distinguished road
Default

I use an NSMutableArray an populate the tableview with it.
Then i delete from the array and populate the tableview again.
Trisect Development is offline   Reply With Quote
Old 08-02-2010, 10:11 AM   #6 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 150
ikaroweb is on a distinguished road
Default

Quote:
Originally Posted by Trisect Development View Post
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.
ikaroweb is offline   Reply With Quote
Old 08-02-2010, 11:32 AM   #7 (permalink)
noob iphone developer
 
Join Date: Jul 2008
Location: Denmark
Posts: 237
Trisect Development is on a distinguished road
Default

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.
Trisect Development is offline   Reply With Quote
Old 08-02-2010, 12:30 PM   #8 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 285
mickm is on a distinguished road
Default

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.
mickm is offline   Reply With Quote
Old 08-02-2010, 01:25 PM   #9 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 150
ikaroweb is on a distinguished road
Default

Quote:
Originally Posted by mickm View Post
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)tableViewUITableView *)tableView commitEditingStyleUITableViewCellEditingStyle)ed itingStyle forRowAtIndexPathNSIndexPath *)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.
ikaroweb is offline   Reply With Quote
Old 08-03-2010, 06:17 AM   #10 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 150
ikaroweb is on a distinguished road
Default

someone help me?
I tried with a new project and get the same error.
ikaroweb is offline   Reply With Quote
Old 08-04-2010, 04:26 PM   #11 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 150
ikaroweb is on a distinguished road
Default

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!
ikaroweb is offline   Reply With Quote
Old 08-07-2010, 11:39 AM   #12 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 285
mickm is on a distinguished road
Default

Quote:
Originally Posted by ikaroweb View Post
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...
mickm is offline   Reply With Quote
Old 08-09-2010, 01:08 AM   #13 (permalink)
Registered Member
 
fousulameen's Avatar
 
Join Date: Jul 2010
Location: chennai
Posts: 41
fousulameen is on a distinguished road
Send a message via Skype™ to fousulameen
Default



just make sure this method is defined or not with out this method row will not be deleted from table

- (void)setEditingBOOL)editing animatedBOOL)animate{

[super setEditing:editing animated:animate];
[self.nametable setEditing:editing animated:YES];
}
fousulameen is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 327
8 members and 319 guests
alexP, gordo26, headkaze, mistergreen2011, nobstudio, rayjeong, Sloshmonster, stanny
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,655
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pungs
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:19 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0