2

I develop an application in which I need to delete the rows from array as well as database..?In cellForRowAtIndexPath I write like cell.textLabel.Text=[myarray.objectAtIndexPath.row]; I Want to delete that row from database and array also.In Editing method I write the code like

MoneyAppDelegate *mydelegate=(MoneyAppDelegate *)[[UIApplication sharedApplication]delegate];

if (editingStyle == UITableViewCellEditingStyleDelete) 
    {
        database *objdata=[[database alloc]init];
        [app deleteCategory:objdata];        
        [self.mytableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}  

But it not work. In deletecategory method I have code like

-(void)deleteCategory:(database *)databaseObj
{
[databaseObj deleteCategory];
 [myarray removeObject:databaseObj];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

MoneyAppDelegate *app = (MoneyAppDelegate *)[[UIApplication sharedApplication]delegate];

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
}

  cell.textLabel.text = [app.myarray4 objectAtIndex:indexPath.row];

return cell;}

deletecategory is the method to delete the record from Database.nothing else. How can i delete that selected row from database I know its very simple but yet I confused..thnanx in advance:)

1
  • thnx for editing i remind it next time Commented Apr 24, 2012 at 5:10

2 Answers 2

2

Try like this-

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
 return UITableViewCellEditingStyleDelete;
}

-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[self delBtnPressed:indexPath.row];
}

//here you place your code-

-(void)delBtnPressed:(int)sender
 {  
NSString *titleStr=@"";
    NSString *table=@"";
NSString *attribute=@"";

sqlite3 *database;

if(![titleStr isEqualToString:@""])
{
    //-------------------------------deletion-------------------------  

    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) 
    {

        NSString *  sqlStatement = [NSString stringWithFormat:@"delete from %@ where %@='%@' ",table,attribute,titleStr];

        if (sqlite3_exec(database, [sqlStatement cStringUsingEncoding:NSUTF8StringEncoding], NULL, NULL, NULL)  == SQLITE_OK)
        {

            [yourArr removeAllObjects];
            [self fetchAgain]; //here you need to fetch all records from DB and fill the yourArr again.
            [tblView reloadData];

        }
        sqlite3_close(database);

    }

}
else 
{
    NSLog(@"Error while deleting please try again");
}

}
Sign up to request clarification or add additional context in comments.

4 Comments

thnx for repl but my code is wrk perfectly..but it just delete from simulator not from database @Naina
I didn't get your problem of working in simulator but not on device..you must be doing something wrong somewhere..you can debug your problem with the help of SMART BREAKPOINTS in XCODE. or if you are using sqlite than you can try with my simple code.
R u in Gmail Or any other Acoount?can we discuss there ?@Naina
u r right m talikng about the sqlite database..i need to delete the record from it..
0

Check this line..

database *objdata=[[database alloc]init];
[app deleteCategory:objdata];  

In the first line you are just creating an object for "database" as "objdata" and alloc it.In this place there will be nothing in "objdata". So when you pass this to deleteCategory: method, there is nothing will get deleted in database. So here is a problem.

Try this:

 database *objdata=[[database alloc]init];
 objdata = [myarray.objectAtIndexPath.row];
[app deleteCategory:objdata];  

5 Comments

it gives error like -[__NSCFString deleteCategory]: unrecognized selector sent to instance 0x6a5fe20 in delegate method @R.A
objdata = [myarray objectAtIndex:indexPath.row]; try like this
paste your cellForRowAtindexPath: method in your question
objdata = [app.myarray4 objectAtIndex:indexPath.row]; did you try like this??
That's Fine. Good. I didn't know that you haven't got the indexPath.row value. Anyway :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.