First of all, I'm new to this so excuse my any simplest question.
I'm trying to view data and filter it which is very common. So I use different tutorials. First, I loaded data from my SQLite database, then I used custom cell tutorials and customized my cells. But I got stuck in UISearchBar. The tutorial I followed uses an NSArray to fill the table which is declared in code-behind. Since my SQLite methods gets the data from database to an Array, I thought if I copy this array to the array in filtering methods, that would work.But it didn't. Here is some code with explanations:-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if ([searchText length] == 0) {
[displayItems removeAllObjects];
[displayItems addObjectsFromArray:allItems];
} else {
[displayItems removeAllObjects];
for (NSString *string in allItems) {
NSRange r = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (r.location != NSNotFound) {
[displayItems addObject:string];
}
}
}
[tableViewScreen reloadData];
}
Code above is for filtering and I tried to copy the array that I used to fill the table to allItems array like this:
MyProjectAppDelegate *appDelegate = (MyProjectAppDelegate *)[[UIApplication sharedApplication] delegate];
allItems = appDelegate.mySqliteArray;
Or like this:
allItems = [[NSArray alloc] initWithArray:appDelegate.mySqliteArray];
But none of them did work.
I'd like to point my problem again,I have a method that gets the data into an array in Ap开发者_高级运维pDelegate class, and in my TableView class, I want to copy that array to another.P.S. mySqliteArray is mutable array and allItems array is not.And also, my cells are created by custom cell class, and there are 2 labels in each row.Since I was defining an array that has objects created by my defined class, I later realized that I was mistaken to carry objects but not strings.
So I updated my code like this and succeeded to create the needed array:
for (MyClass *object in appDelegate.mySqliteArray) {
[allItems addObject:[NSString stringWithFormat:@"%@", object.objectName]];
}
精彩评论