2012-03-09 3 views
0

Я столкнулся с этой проблемой последние два дня, и я не могу понять, как это понять. У меня есть база данных SQlite со следующей структурой.iOS NSArray из NSDictionaries из SQLite в UITableView

enter image description here

это один ко многим отношений между списком и List_Items

я получить доступ к базе данных и создать объект, который затем добавляют к NSDictionary, который добавляется к NSArray. Я делаю это дважды, один раз для таблицы List и один раз для List_Items. Затем я использую список Array для подсчета количества списков для строк таблицы tableview, затем добавляю их в tableview.

Проблема возникает, когда я к методу

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

Я не могу показаться, чтобы выяснить, как совпасть записи из списков и List_Items, чтобы показать элементы списка, которые относятся к этому списку в развернутый стол.

Конкретные примеры кода было бы полезно, как мой ум кашицу на данный момент :(

Вот соответствующий код, который у меня до моей нынешней 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); 
    } 

} 

EDIT **** ******

сингл Sql оператор возвращает более одного ИМЯ_СПИСКА. Это проблема, потому что мне нужно только один из каждого имени списка.

enter image description here

ответ

0

всех, кто заинтересован в этом, как я ней- до выяснить это. Я получаю списокID для списка, затем я делаю массив и перебираю элементы списка, сопоставляя их с идентификатором listID. Если они совпадают с идентификатором listID, я добавляю их в массив, и как только он заканчивается циклом for, он добавляет результаты в мой протокол передачи данных (чтобы сделать его доступным для следующего представления), я представляю контроллер модального представления и загружаю массив из объекта данных. Как только я закончил с модальным видом, я поставил объект данных обратно в nil и 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, а затем создаете объект NSDictionary, который почти такой же, как объект List. Зачем? Почему бы просто не добавить объект List в массив. Если вы не выполняете какие-либо функции в свойствах в элементе списка, тогда вообще не используйте объект List, просто поместите поля непосредственно в NSDictionary.

Во-вторых, не делайте двух разных вызовов SQL, чтобы получить информацию, используйте только одно, чтобы одновременно получить список и список_имя для этого списка. Затем, если вы используете объект List, добавьте элементы вызова свойств NSMutableArray и добавьте свои элементы listItems в этот массив. Вы можете сделать то же самое в NSDictionary, просто добавьте объект NSMutableArray для ключевых элементов, а затем добавьте list_items в этот массив.

Теперь вы сможете настроить представление таблицы, чтобы делать то, что вы хотите.

Измененный ответ в ответ на комментарии ниже

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

+0

я их в том же запросе, но прежде, чем я получаю слишком много объектов назад, потому что это один ко многим отношений так, например, я хотел бы получить обратно 3 объектов, которые все имели тот же ИМЯ_СПИСОК, но разные элементы. поэтому, когда я добавил Listname в tableview, у меня было бы три одинакового имени listName, потому что в нем было три элемента. – KMG

+0

Yup Я возвращаюсь 7 из каждого listName, когда я делаю NSLog в словаре, мне действительно нужен только один. – KMG

+0

Это мой SQL-запрос для обоих списков «const char * sqlStatement =« SELECT * from List, List_Items »; – KMG

Смежные вопросы