【问题标题】:Delete rows from array & database从数组和数据库中删除行
【发布时间】:2012-05-04 17:29:58
【问题描述】:

我开发了一个应用程序,在该应用程序中我需要从数组和数据库中删除行..?在 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 提前:)

【问题讨论】:

  • 谢谢编辑,下次提醒

标签: objective-c ios cocoa-touch uitableview


【解决方案1】:

试试这样-

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

}

【讨论】:

  • thnx for repl 但我的代码是完美的..但它只是从模拟器中删除而不是从数据库@Naina
  • 我没有得到你在模拟器上工作但在设备上工作的问题..你一定在某处做错了..你可以在 SMART BREAKPOINTS 的帮助下调试你的问题 在 XCODE 中。或者,如果您使用的是 sqlite,则可以尝试使用我的简单代码。
  • R u 在 Gmail 或任何其他帐户中?我们可以在那里讨论吗?@Naina
  • 你说得对,关于 sqlite 数据库..我需要从中删除记录..
【解决方案2】:

检查这一行..

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

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

试试这个:

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

【讨论】:

  • 它给出的错误类似于 -[__NSCFString deleteCategory]: unrecognized selector sent to instance 0x6a5fe20 in delegate method @R.A
  • objdata = [myarray objectAtIndex:indexPath.row];试试这样
  • 在你的问题中粘贴你的 cellForRowAtindexPath: 方法
  • objdata = [app.myarray4 objectAtIndex:indexPath.row];你试过这样吗??
  • 没关系。好的。我不知道您没有 indexPath.row 值。无论如何:-)
猜你喜欢
  • 2015-08-07
  • 2011-11-10
  • 2015-02-11
  • 2019-01-04
  • 1970-01-01
  • 1970-01-01
  • 2018-12-19
  • 2014-07-22
相关资源
最近更新 更多