objective-c - 从数组和数据库中删除行

标签 objective-c ios cocoa-touch uitableview

我开发了一个应用程序,需要从数组和数据库中删除行..?在 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 是从数据库中删除记录的方法。没有别的。 我如何从数据库中删除选定的行我知道它很简单但我很困惑..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");
}

}

关于objective-c - 从数组和数据库中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10291802/

相关文章:

objective-c - 在 viewDidUnload() 中设置为 nil

objective-c - 将 Swift Dictionary 转换为 NSMutableDictionary 的这两个选项有什么区别?

ios - 如何在 Swift 中的 TextView 旁边添加一个按钮(以编程方式或在 Storyboard 中)?

ios - 关于在应用程序启动结束时有 Root View Controller 的错误消息

iphone - 哪些应用程序启动到后台状态?

objective-c - 获取向量中的所有点(两点之间)

ios - 基于 ScrollView Position 的 UIImageView Fading

iphone - "zero"是 cocoa 中的保留标签号吗?

ios - 仅在 iOS 16.1 中创建状态变量?

IOS - 使用 REST API 的基本身份验证 - SSL - AFNetworking 3.0