2012-03-09 36 views
0

我一直在努力争取过去两天这个问题,我似乎无法弄清楚。我有一个具有以下结构的SQlite数据库。iOS的NSArray从SQLite在UITableView的NSDictionaries

enter image description here

它是一个列表和List_Items

我访问数据库,并创建一个对象,然后将其添加到被添加到一个的NSArray NSDictionary的之间一对多的关系。我做了两次,一次用于List表,一次用于List_Items。 然后我使用列表数组来计算我的tableview行的列表数,然后将它们添加到tableview。

的问题出现时,我得到的方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

我似乎无法弄清楚如何匹配的列表和List_Items的记录,以显示属于该名单列表中的项目一个向下钻取tableview。因为我的心在这一点玉米粥

具体代码示例会有帮助:(

下面是相关的代码,我有多达我目前Writersblock。

//#******************************************************# 

//    *******Start Database******* 

//#******************************************************# 

-(void)checkAndCreateDatabase 
{ 
    // Check if the SQL database has already been saved to the users phone, if not then copy it over 
    BOOL success; 

    // Create a FileManager object, we will use this to check the status 
    // of the database and to copy it over if required 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    // Check if the database has already been created in the users filesystem 
    success = [fileManager fileExistsAtPath:databasePath]; 

    // If the database already exists then return without doing anything 
    if(success) return; 

    // If not then proceed to copy the database from the application to the users filesystem 

    // Get the path to the database in the application package 
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName]; 

    // Copy the database from the package to the users filesystem 
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; 
} 

-(void)readItemsFromDatabase 
{ 
    // Setup the database object 
    sqlite3 *database; 

    // Init the Items Array 
    items = [[NSMutableArray alloc] init]; 
    lists = [[NSMutableArray alloc] init]; 

    //---------------### SELECT THE LISTS #####---------------// 

    // Open the database from the users filessytem 
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) 
    { 
     NSLog(@"SQL Opened"); 
     // Setup the SQL Statement and compile it for faster access 
     const char *sqlStatement = "SELECT * from List"; 
     sqlite3_stmt *compiledStatement; 
     if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { 
      // Loop through the results and add them to the array 
      while(sqlite3_step(compiledStatement) == SQLITE_ROW) { 
       // Read the data from the result row 
       NSString *aListName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; 
       NSString *aUserID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)]; 
       NSString *aListID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)]; 

       NSLog(@"SQL Compiled"); 

       // Create a new list object with the data from the database 
       List *list = [[List alloc] initWithlistName:(NSString *)aListName userID:(NSString *)aUserID listID:(NSString *)aListID]; 

       listNames = [NSDictionary dictionaryWithObjectsAndKeys:list.listName,@"listName",list.listID,@"listID",list.listID,@"listID",nil]; 

       // Add the Shopping object to the list Array 
       [lists addObject:listNames]; 

      } 
     } 

     else { NSLog(@"Database Not Found");} 

     // Release the compiled statement from memory 
     sqlite3_finalize(compiledStatement); 
    } 
    sqlite3_close(database); 

     //---------------### SELECT THE LIST_ITEMS #####---------------// 

     if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) 
     { 
      NSLog(@"SQL Opened"); 
      // Setup the SQL Statement and compile it for faster access 
      const char *sqlStatement = "SELECT * from List_Items"; 
      sqlite3_stmt *compiledStatement; 
      if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { 
       // Loop through the results and add them to the array 
       while(sqlite3_step(compiledStatement) == SQLITE_ROW) { 
        // Read the data from the result row 

        NSString *aBrandName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; 
        NSString *aItemName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)]; 
        NSString *aItemQuantity = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)]; 
        NSString *aImageUrl = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)]; 
        NSString *aListID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 6)]; 

        NSLog(@"SQL Compiled"); 

        // Create a new items object with the data from the database 
        Shopping *shopping = [[Shopping alloc] initWithlistID:(NSString *)aListID brandName:(NSString *)aBrandName itemName:(NSString *)aItemName itemQuantity:(NSString *)aItemQuantity imageURL:(NSString *)aImageUrl];      

        itemList = [NSDictionary dictionaryWithObjectsAndKeys:shopping.listID,@"listID",shopping.brandName,@"brandName",shopping.itemName,@"itemName",shopping.itemQuantity,@"itemQuantity",shopping.imageURL,@"imageURL",nil]; 

        // Add the Shopping object to the items Array 
        [items addObject:itemList]; 
       } 
      } 

      else { NSLog(@"Database Not Found");} 

      // Release the compiled statement from memory 
      sqlite3_finalize(compiledStatement); 

     NSLog(@"%@",items); 
     NSLog(@"%@",lists); 

    } 
    sqlite3_close(database); 

} 

//#******************************************************# 

//    *******END Database******* 

//#******************************************************# 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    int rowcount; 
    rowcount = [lists count]; 
    return rowcount; 
} 

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:CellIdentifier];  
    } 

    // Set up the cell... 

    NSString *cellValue = [[lists objectAtIndex:indexPath.row] objectForKey:@"listName"]; 

    cell.textLabel.text = cellValue; 

    return cell; 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if ([[lists objectAtIndex:indexPath.row] objectForKey:@"listID"] != NULL) 
    {   
     NSString *listIndex = [[lists objectAtIndex:indexPath.row] objectForKey:@"listID"]; 
     int i = [listIndex intValue]; 
     NSLog(@"indexPath: %d",i); 
    } 

} 

编辑**** ******

的单个SQL语句返回多个列表名,这是一个问题,因为我只需要每个列表的名字之一。

enter image description here

回答

0

任何有兴趣我这是怎么了ened计算出来。 我得到列表的listID,然后我创建一个数组并循环列表项,并将它们与listID进行比较。如果它们与listID相同,我将它们添加到数组中,一旦它完成for循环,它会将结果添加到我的数据对象协议(以使其可用于下一个视图),然后提交一个模式视图控制器并加载来自数据对象的数组。一旦我完成了模态视图,我将数据对象重新设置为零和Tada!有用。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if ([[lists objectAtIndex:indexPath.row] objectForKey:@"listID"] != NULL) 
    {   
     [listTableView deselectRowAtIndexPath:indexPath animated:YES]; 
     NSString *listIndex = [[lists objectAtIndex:indexPath.row] objectForKey:@"listID"]; 

     NSMutableArray *itemArray = [[NSMutableArray alloc]init]; 

     int i; int itemCount = [items count]; 

     for (i = 0; i < itemCount; i++) 
     { 
      if ([[[items objectAtIndex:i] objectForKey:@"listID"] isEqual:listIndex]) 
      { 
       [itemArray addObject:[items objectAtIndex:i]]; 
       NSLog(@"Item Array:%@",itemArray); 
      } 
     } 

     if (i == itemCount) 
     { 
      AppDataObject* theDataObject = [self theAppDataObject]; 
      theDataObject.itemArray = itemArray; 

      ItemView *temp = [[ItemView alloc] initWithNibName:@"ItemView" bundle:nil]; 
      [self presentModalViewController: temp animated: YES]; 
     } 

    } 

} 
1

因此,首先创建一个List对象,然后创建一个与List对象非常相似的NSDictionary对象。为什么?为什么不直接将List对象添加到数组中。如果您没有对List项目的属性执行任何功能,那么根本不要使用List对象,只需将这些字段直接放在NSDictionary中即可。

其次,不要做两个不同的SQL调用来获取信息,只能使用一个来同时获取该列表的list和list_items。然后,如果您使用的是List对象,请添加一个NSMutableArray属性调用项目,然后将listItems添加到该数组中。你可以在NSDictionary中做同样的事情,只需为关键项添加一个NSMutableArray对象,然后将list_items添加到该数组中。

现在你可以设置tableview来做你想做的事情。响应

修订回答下面的评论

Select * FROM List, List_Items WHERE List.list_id = List.list_id

+0

我在同一个查询在他们面前,但我得到了太多的对象回来,因为它是一个一对多的关系,例如我还是会回到3个对象,所有具有相同LISTNAME但不同的项目。所以当我将Listname添加到tableview时,我将拥有三个相同的listName,因为它有三个项目。 – KMG 2012-03-09 05:26:12

+0

是的,当我在字典上进行NSLog时,我每次都返回7个listName,我只需要每个列表中的一个。 – KMG 2012-03-09 05:35:51

+0

这是我的两个列表的SQL查询“const char * sqlStatement =”SELECT * from List,List_Items“;” – KMG 2012-03-09 05:36:50