iOS NSМассив NSDictionaries из SQLite в UITableView - PullRequest
0 голосов
/ 09 марта 2012

Последние два дня я боролся с этой проблемой, и я не могу понять, как ее решить.У меня есть база данных SQlite со следующей структурой.

enter image description here

это отношение один ко многим между List и List_Items

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

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

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

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

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

Вот соответствующий код, который у меня есть до моего текущего блока 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

Ответы [ 2 ]

1 голос
/ 09 марта 2012

Итак, прежде всего, вы создаете объект List, а затем создаете объект NSDictionary, который в значительной степени совпадает с объектом List.Зачем?Почему бы просто не добавить объект List в массив.Если вы не выполняете никаких функций для свойств в элементе List, тогда вообще не используйте объект List, просто поместите поля непосредственно в NSDictionary.

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

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

Исправленный ответ в ответ на комментарии ниже

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

0 голосов
/ 11 марта 2012

Кто-нибудь заинтересовался, вот как я выяснил это.Я получаю listID для списка, затем создаю массив и перебираю элементы списка, сравнивая их со списком ID.Если они совпадают с 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];
        }

    }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...