2

我开发了一个应用程序,在该应用程序中我需要从数组和数据库中删除行......?在cellForRowAtIndexPath中,我写得像cell.textLabel.Text=[myarray.objectAtIndexPath.row]; 我也想从数据库和数组中删除该行。在编辑方法中,我编写如下代码

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];
}  

但它不起作用。在 deletecategory 方法中,我有类似的代码

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

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

{

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

静态 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 是从 Database.nothing 中删除记录的方法。我如何从数据库中删除选定的行我知道它很简单但我很困惑..thnanx 提前:)

4

2 回答 2

2

试试这样-

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

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

//在这里你放置你的代码-

-(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");
}

}
于 2012-04-24T05:42:06.593 回答
0

检查这条线..

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

在第一行中,您只是为“”创建一个对象database作为“ objdata”并分配它。在这个地方,“”中将没有任何内容objdata。因此,当您将其传递给deleteCategory: 方法时,数据库中不会删除任何内容。所以这里有一个问题。

试试这个:

 database *objdata=[[database alloc]init];
 objdata = [myarray.objectAtIndexPath.row];
[app deleteCategory:objdata];  
于 2012-04-24T07:24:47.090 回答