Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-10-2011, 03:43 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 26
richammond is on a distinguished road
Unhappy TableView Search Question : SIGBART ERROR

Hi,

Having a bit of trouble with a specific piece of code. I can implement the UISearch which works well but when there's a character contained in several fields, i get an index out of bounds error. Here's what i mean :

I've got a table with the following fields and some dummy data as follows :

------------
id | word | keywords| definition
1 | w1 | wessai, testing | trying something
2 | d2 | work, code | cloloured work
------------

The current tableview displays the items in its row as follows
w1 (cell.textLabel.text)
essai,testing (cell.detailTextLabel.text)

Up to here, everything's FINE. When i do the search, i can find what i want, so if i type "o", i'll see :
d2
work, code
Which is fine. But when i type "w" i get a sigbart error, with an index out of bounds :-(

Here's what I'm trying to achieve :
Search through word AND keywords AND definition at the same time

And here's my code which handles the search term and puts it in an array:

Code:
- (void)handleSearchForTerm:(NSString *)searchTerm
{
	
	[self setSavedSearchTerm:searchTerm];
	
	if ([self searchResults] == nil)
	{
		NSMutableArray *array = [[NSMutableArray alloc] init];
		[self setSearchResults:array];
		[array release], array = nil;
	}
	
	[[self searchResults] removeAllObjects];
    
    //test begin
    if ([self searchResults_2] == nil)
	{
		NSMutableArray *array = [[NSMutableArray alloc] init];
		[self setSearchResults_2:array];
		[array release], array = nil;
	}
	
	[[self searchResults_2] removeAllObjects];
    //test end
    
	
	if ([[self savedSearchTerm] length] != 0)
	{
		for (NSString *currentString in glossaryListItems_2)
		{
			if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
			{
				[[self searchResults] addObject:currentString];  //add words to search results array
			}
		}
        
        for (NSString *currentString in glossaryKeywords)
		{
			if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
			{
				[[self searchResults_2] addObject:currentString]; //add keywords to search results 2 array
			}
		}
	}
    
}

Here's what inside my
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView
		 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Code:
	if (tableView == [[self searchDisplayController] searchResultsTableView]){
        
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        
        //test 
        NSString *searchedValue;
        NSString *searchedValue_2;
        if ([searchResults count] != 0) {
            @try {
                searchedValue = [searchResults objectAtIndex:indexPath.row]; //typed value
            }
            @catch (NSException* ex) {
                NSLog(@"exception catched on SR1: %@",ex);
                searchedValue = [searchResults objectAtIndex:0];
            }
             
            
        }
        
        if ([searchResults_2 count] != 0) {
            @try {
                searchedValue_2 = [searchResults_2 objectAtIndex:indexPath.row]; //typed value
            }
            @catch (NSException* ex) {
                NSLog(@"exception catched on SR2: %@",ex);
                searchedValue_2 = [searchResults objectAtIndex:0];
            }  
        }
        //test end
        

       
                
        //let's retrieve short description from DB - begin
        sqlite3 *db;
        int dbrc; // database return code
        ikoi_2AppDelegate *appDelegate = (ikoi_2AppDelegate*)
        [UIApplication sharedApplication].delegate;
        const char* dbFilePathUTF8 = [appDelegate.dbFilePath UTF8String];
        dbrc = sqlite3_open (dbFilePathUTF8, &db);
        if (dbrc) {NSLog (@"couldn't open db:");}
        sqlite3_stmt *dbps;
        
        NSString *queryStatementNS;
        
        if (([searchResults count] == 0) && [searchResults_2 count] != 0){
        queryStatementNS = [NSString stringWithFormat:@"SELECT * FROM glossary WHERE keywords LIKE \"%@\"",searchedValue_2];
        }else if (([searchResults count] != 0) && [searchResults_2 count] == 0) {
        queryStatementNS = [NSString stringWithFormat:@"SELECT * FROM glossary WHERE word LIKE \"%@\"",searchedValue];   
        }else if (([searchResults count] != 0) && [searchResults_2 count] != 0) {
        queryStatementNS = [NSString stringWithFormat:@"SELECT * FROM glossary WHERE word LIKE \"%@\" OR keywords LIKE \"%@\"",searchedValue,searchedValue_2];
        }

        
        const char *queryStatement = [queryStatementNS UTF8String];
        sqlite3_prepare_v2 (db, queryStatement, -1, &dbps, NULL);
        NSLog(@"query : %@",queryStatementNS);
        
        while (sqlite3_step (dbps) == SQLITE_ROW) {
        NSString *db_word = [[NSString alloc] initWithUTF8String: (char*) sqlite3_column_text (dbps,1)];
        NSString *db_keywords = [[NSString alloc] initWithUTF8String: (char*) sqlite3_column_text (dbps, 2)];
            
            cell.textLabel.text = db_word;
            cell.detailTextLabel.text = [NSString stringWithFormat:@"Synonyms : %@",db_keywords];               
        
            [db_word release];
            [db_keywords release];

        }
        
        
        sqlite3_finalize (dbps);
        sqlite3_close(db);
        //let's retrieve short description from db - end
      
        
    }
Any help will be greatly appreciated
richammond is offline   Reply With Quote
Reply

Bookmarks

Tags
search, search display controller, tableview, uisearch

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 400
19 members and 381 guests
13dario13, 7twenty7, buggen, eski, EvilElf, glenn_sayers, HemiMG, jarv, LunarMoon, morterbaher, n00b, pbart, Pudding, QuantumDoja, sacha1996, Sami Gh, UMAD, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,673
Threads: 94,122
Posts: 402,906
Top Poster: BrianSlick (7,990)
Welcome to our newest member, morterbaher
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:22 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0