Objective c 从数组中删除行(&A);数据库
我开发了一个应用程序,需要从数组和数据库中删除行..?在cellforrowatinexpath中,我像cell.textlab.Text=[myarray.objectAtIndexPath.row]那样编写我也想从数据库和数组中删除该行Objective c 从数组中删除行(&A);数据库,objective-c,ios,cocoa-touch,uitableview,Objective C,Ios,Cocoa Touch,Uitableview,我开发了一个应用程序,需要从数组和数据库中删除行..?在cellforrowatinexpath中,我像cell.textlab.Text=[myarray.objectAtIndexPath.row]那样编写我也想从数据库和数组中删除该行 MoneyAppDelegate *mydelegate=(MoneyAppDelegate *)[[UIApplication sharedApplication]delegate]; if (editingStyle == UITableViewCel
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*应用=(MoneyAppDelegate*)[[UIApplication sharedApplication]委托]
静态NSString*CellIdentifier=@“Cell”
deletecategory是从数据库中删除记录的方法。没有其他方法。
如何从数据库中删除所选的行我知道这很简单,但我很困惑..thnanx预先:)像这样试试-
- (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");
}
}
检查这一行
database *objdata=[[database alloc]init];
[app deleteCategory:objdata];
在第一行中,您只是将“数据库”的对象创建为“objdata
”并将其分配。在这里,“objdata
”中没有任何内容。因此,当您将其传递给deleteCegory
:方法时,数据库中没有任何内容将被删除。因此出现了一个问题
试试这个:
database *objdata=[[database alloc]init];
objdata = [myarray.objectAtIndexPath.row];
[app deleteCategory:objdata];
thnx用于编辑,我下次提醒它thnx用于repl,但我的代码编写得很好。但它只是从模拟器中删除而不是从数据库中删除@nai没有遇到在模拟器中工作的问题,但不是在设备上工作的问题。你一定是在某个地方出错了。你可以在XCODE中使用智能断点来调试你的问题。或者如果你正在使用sql它比你可以尝试使用我的简单代码更简单。Gmail中的RU或任何其他acount?我们可以在那里讨论吗?@Nainau R right m taliking关于sqlite数据库的信息..我需要从中删除记录..它会给出类似-[\uu NSCFString deleteCategory]:委托方法中发送给实例0x6a5fe20的无法识别的选择器@R.Aobjdata=[myarray objectAtIndex:indexPath.row];像这样尝试在问题中粘贴cellForRowAtindexPath:method objectAtIndex=[app.myarray4 objectAtIndex:indexPath.row];你是这样尝试的吗?很好。很好。我不知道你没有indexPath.row值。总之:-)
database *objdata=[[database alloc]init];
objdata = [myarray.objectAtIndexPath.row];
[app deleteCategory:objdata];