我一直在努力爭取過去兩天這個問題,我似乎無法弄清楚。我有一個具有以下結構的SQlite數據庫。iOS的NSArray從SQLite在UITableView的NSDictionaries
它是一個列表和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語句返回多個列表名,這是一個問題,因爲我只需要每個列表的名字之一。
我在同一個查詢在他們面前,但我得到了太多的對象回來,因爲它是一個一對多的關係,例如我還是會回到3個對象,所有具有相同LISTNAME但不同的項目。所以當我將Listname添加到tableview時,我將擁有三個相同的listName,因爲它有三個項目。 – KMG 2012-03-09 05:26:12
是的,當我在字典上進行NSLog時,我每次都返回7個listName,我只需要每個列表中的一個。 – KMG 2012-03-09 05:35:51
這是我的兩個列表的SQL查詢「const char * sqlStatement =」SELECT * from List,List_Items「;」 – KMG 2012-03-09 05:36:50