Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

TableView Search Question : SIGBART ERROR

richammondrichammond Posts: 26Registered Users
edited October 2011 in iPhone SDK Development
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:


- (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
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{



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
Sign In or Register to comment.