Cocoa 如何在TableView中标识项ID并删除数据库

Cocoa 如何在TableView中标识项ID并删除数据库,cocoa,tableview,fmdb,Cocoa,Tableview,Fmdb,我是新来的,还有一些疑问。 我创建了一个类,用于将数据从数据库表加载到数组中。现在,我想通过在TableView中选择Delete来实现从数据库中删除记录。我有麻烦了 我无法从数据库inTableView中显示项目的描述。字段id显示,但“字段名”不显示 这就好像数组中不存在对象一样 我遇到的另一个问题是如何删除数据库 //Variáveis para coleta de dados do banco NSString *ID_KEY = @"_id"; NSString *NAME_KE

我是新来的,还有一些疑问。 我创建了一个类,用于将数据从数据库表加载到数组中。现在,我想通过在TableView中选择Delete来实现从数据库中删除记录。我有麻烦了

我无法从数据库inTableView中显示项目的描述。字段id显示,但“字段名”不显示

这就好像数组中不存在对象一样

我遇到的另一个问题是如何删除数据库

//Variáveis para coleta de dados do banco
NSString *ID_KEY    = @"_id";
NSString *NAME_KEY  = @"name";
NSString *DESCRIPTION_KEY = @"description";

#pragma mark - Métodos Usuários
- (NSArray *)GetCategories
{
    //Cria uma instância do DataBase para poder trabalhar
    FMDatabase *db = [Database DBWrite];
    //Cria um array para receber os dados do select
    NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
    //Testamos se o DataBase está aberto
    if ([db open]){
        //Precisamos de um ResultSet para receber e trabalhar com o resultado do SELECT
        FMResultSet *rs = [db executeQuery:@"SELECT _id, name, description FROM category"];
        //Fazemos um LOOP para carregar o array com os dados do SELECT
        while ([rs next]) {
            //Campo _id
            int _idI = [rs intForColumn:@"_id"];
            NSNumber *fieldID = [[NSNumber alloc] initWithInt:_idI];

            //Campo name
            NSString *fieldName = [rs stringForColumn:@"name"];


            //Campo description
            NSString *fieldDescription = [rs stringForColumn:@"description"];            

            //Dicionário para guardar as chaves e objetos
            NSMutableDictionary *rowDict = [[NSMutableDictionary alloc] initWithCapacity:3];

            //Guardamos as informações do ID e Name no Log para futura consulta em caso de erros
            //NSLog(@"%@ - %@ - %@", fieldID, fieldName, fieldDescription);            

            //Adiciona os valores e chaves de cada linha
            [rowDict setObject:fieldID          forKey: ID_KEY];
            [rowDict setObject:fieldName        forKey: NAME_KEY];
            [rowDict setObject:fieldDescription forKey: DESCRIPTION_KEY];

            NSLog(@"%@ - %@ - %@", [rowDict objectForKey:ID_KEY], [rowDict objectForKey:NAME_KEY], [rowDict objectForKey:DESCRIPTION_KEY]);             

            [array addObject:rowDict];

            [fieldID release];
            [fieldName release];
            [fieldDescription release]; 
            [rowDict release];
        }
        [rs close];
        [db close];
    }
    return array;
}



-(void)viewWillAppear:(BOOL)animated
{
   [super viewWillAppear:animated];
    //Cria e inicializa a classe categoria
    Category *categoria = [[Category alloc] init];
    //Verifica se o array está vazio e inicializado e cria
    if (arrCategory != nil)
        [arrCategory release];
    //Carrega os dados da categoria

    arrCategory = [[NSMutableArray alloc] initWithArray:[categoria GetCategories]];
    //Inicializa tudo

    [categoria release];
    [tbvCategory reloadData];
}



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

{

    //Cria a célula
    UITableViewCell *celula = [tableView dequeueReusableCellWithIdentifier:@"celula"];
    if (celula == nil) {
        celula = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"celula"] autorelease];
        //Define o estilo da célula
        [celula setSelectionStyle:UITableViewCellSelectionStyleGray];
    }

    NSDictionary *rowVals = (NSDictionary *) [arrCategory objectAtIndex:indexPath.row];
    //Pega o valor do campo Name
    NSString *fieldName = (NSString *) [rowVals objectForKey:@"name"];

    NSLog(@"%@ - %@", [rowVals objectForKey:@"name"], fieldName);

    //Here is ERROR
    celula.textLabel.text = [NSString stringWithFormat:@"%@", [rowVals objectForKey:@"name"]];

    return celula;
}



- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{    

    Category *categoria = [[Category alloc] init];
    NSDictionary *dici = (NSDictionary *) [arrCategory objectAtIndex:indexPath.row];
    NSNumber *linha = (NSNumber *) [dici objectForKey:@"_id"];

    NSNumber *selRow = [[NSNumber alloc] initWithInteger: indexPath.row];

    [categoria DeleteCategory:selRow];

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    [arrCategory removeObjectAtIndex:indexPath.row];
    [tableView endUpdates];
    [tableView reloadData];    

}
请尝试以下操作:

- (NSArray *)GetCategories
{
 NSString* dbPath = @"Path To Database";
 FMDatabase *db = [FMDatabase databaseWithPath:dbPath];

 NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
 if (![db open]){
    NSLog(@"Could not open DB");
    return nil;
 }
 FMResultSet *rs = [db executeQuery:@"SELECT _id, name, description FROM category"];
 while ([rs next]) 
 {
     int _idI = [rs intForColumn:@"_id"];
     NSNumber *fieldID = [NSNumber numberWithInt:_idI];
     NSString *fieldName = [rs stringForColumn:@"name"];
     NSString *fieldDescription = [rs stringForColumn:@"description"];            

     NSMutableDictionary *rowDict = [[NSMutableDictionary alloc] initWithCapacity:3];

     //Adiciona os valores e chaves de cada linha
     [rowDict setObject:fieldID          forKey: ID_KEY];
     [rowDict setObject:fieldName        forKey: NAME_KEY];
     [rowDict setObject:fieldDescription forKey: DESCRIPTION_KEY];          

     [array addObject:rowDict];
     [rowDict release];
 }
 [rs close];
 [db close];

 return array;
}

NSLogs是否从GetCategories方法内部正确打印?顺便说一句,对方法名使用camel大小写是一种很好的做法,并且只以大写字母开头类名。