Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 从sqlite到tableview_Objective C_Sqlite_Tableview - Fatal编程技术网

Objective c 从sqlite到tableview

Objective c 从sqlite到tableview,objective-c,sqlite,tableview,Objective C,Sqlite,Tableview,我对objective-c很陌生,所以我有几个问题。 如何从(SQLite)数据库中获取数据以显示在表视图中。实际上,我现在可以从表中选择数据,但我真的不知道如何将数据库表中的项目名称添加到我的tableview中 在第一个问题之后,如何单击根据第一个问题命名的tableview单元格,然后移动到另一个控制器,该控制器在该视图上显示项目的名称和说明 在我的loaddata方法中,我已经可以调用并从表中选择项目,并将其显示在NSLog上 你们能告诉我怎么做吗,或者给我一些教程或链接,我可以看看并进

我对objective-c很陌生,所以我有几个问题。

  • 如何从(SQLite)数据库中获取数据以显示在表视图中。实际上,我现在可以从表中选择数据,但我真的不知道如何将数据库表中的项目名称添加到我的tableview中

  • 在第一个问题之后,如何单击根据第一个问题命名的tableview单元格,然后移动到另一个控制器,该控制器在该视图上显示项目的名称和说明

  • 在我的loaddata方法中,我已经可以调用并从表中选择项目,并将其显示在NSLog上

    你们能告诉我怎么做吗,或者给我一些教程或链接,我可以看看并进一步研究我想要什么

    谢谢。



    请找到问题的答案

  • 从SQLLite DB获得的数据可以存储在NSArray或NSDictionary中,也可以使用NSArray或NSDictionary将其加载到UITableView中的方法“cellForRowAtIndexPath”,而不是使用table_菜单

  • 您需要遵守UITableView委托方法,并在方法“didSelectRowatineXpath”中检查该特定行,并从SQLLite DB获取详细信息。如果您使用的是故事板,您可以使用[self-PerformsgueWithIdentifier:@“segue-identified-sender:self];并在方法“-(void)prepareforsgue:(UIStoryboardSegue*)中使用相同的处理方法

    (void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath { NSInteger rowSel=indexPath.row

     //Get the details from SQLLite DB
     [self performSegueWithIdentifier:@"segue identifier" sender:self];
    
    }

    (void)prepareForSegue:(UIStoryboardSegue*)segue发送方:(id)发送方 { if([segue.identifier IsequalString:@“segue identifier”]) { UIViewController*控制器=(UIViewController*)segue.destinationViewController; } }


  • 希望这有帮助。

    例如,我认为您需要这样的方法。
    TableViewController⇔ 详细数据控制器

    NSObject*数据

    @property NSInteger dataId;
    @pproperty NSString* title;
    
    视图控制器

    - (NSArray *)arr
    {
        FMDatabase* db = [self getConnection];
        [db open];
    
        FMResultSet*    results = [db executeQuery:SQL_SELECT];
        NSMutableArray* books = [[NSMutableArray alloc] initWithCapacity:0];
    
        while( [results next] )
        {
            Data* data = [[Book alloc] init];
            data.dataId    = [results intForColumnIndex:0];
            data.title    = [results stringForColumnIndex:1];
    
            [arr addObject:data];
        }
        [db close];
        return arr;
    }
    
    - (Data*)dataAtIndexPath:(NSIndexPath*)indexPath
    {
        return [arr objectAtIndex:indexPath.row];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //cellIdentifier etc
    
        Data* data = [self dataAtIndexPath:indexPath];
        cell.textLabel.text = data.title;
    
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        DetailDataController* detail = [[DetailDataController alloc] initWithNibName:@"DetailData" bundle:nil];
        detail.delegate = self;
        detail.data     = [self dataAtIndexPath:indexPath];
    
        [self.navigationController pushViewController:detail animated:YES];
    }
    
    - (NSArray *)arr
    {
        FMDatabase* db = [self getConnection];
        [db open];
    
        FMResultSet*    results = [db executeQuery:SQL_SELECT];
        NSMutableArray* books = [[NSMutableArray alloc] initWithCapacity:0];
    
        while( [results next] )
        {
            Data* data = [[Book alloc] init];
            data.dataId    = [results intForColumnIndex:0];
            data.title    = [results stringForColumnIndex:1];
    
            [arr addObject:data];
        }
        [db close];
        return arr;
    }
    
    - (Data*)dataAtIndexPath:(NSIndexPath*)indexPath
    {
        return [arr objectAtIndex:indexPath.row];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //cellIdentifier etc
    
        Data* data = [self dataAtIndexPath:indexPath];
        cell.textLabel.text = data.title;
    
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        DetailDataController* detail = [[DetailDataController alloc] initWithNibName:@"DetailData" bundle:nil];
        detail.delegate = self;
        detail.data     = [self dataAtIndexPath:indexPath];
    
        [self.navigationController pushViewController:detail animated:YES];
    }