views:

57

answers:

2

I have a UITableView that was created from data coming from a mutable array.

This is an array of dictionaries. So, in order to populate my table, I did something like

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

...

NSDictionary *umObject = (NSDictionary*)[listaDeObjectos objectAtIndex:indexPath.row];
NSString *oneName = [umObject objectForKey:@"name"];

[cell setText:oneName];

...

}

This is working fine, but now I am trying to implement a search on this table. I read a tutorial where the guy uses a search method like this...

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [searchedData removeAllObjects];// remove all data that belongs to previous search
    if([searchText isEqualToString:@""] || searchText==nil){
        [myTableView reloadData];
        searching = NO;
        return;
    }

    searching = YES;
    NSInteger counter = 0;
    for(NSString *name in dataSource)
    {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
        NSRange r = [name rangeOfString:searchText];
        if(r.location != NSNotFound)
                [searchedData addObject:name];
        counter++;
        [pool release];
    }
    [myTableView reloadData];
}

as you can see, he has this line

for(NSString *name in dataSource)

where name is an entry from dataSource (??)

This sounds like the table being populated from some kind of array, but, as far as I know, my table was not populated from an array directly, but instead, from values I extracted on the first part of my code, one by one.

I am not sure if I understood the concept of datasource.

I have read the docs but I am still not understanding that.

What is this datasource? Does my table has one? If not, how do I search my table?

Cay you guys help?

thanks

+2  A: 

Your datasource is your class where you have all this UITableView related methods (tableView:cellForRowAtIndexPath: etc.). This class must adopt UITableViewDataSource protocol.

When you provide data for each of your cell, you take it from your array listaDeObjectos. When you search, you want to provide only several items from your array, these items must adopt your search criteria.

So when you are not in "search mode", you must provide data from your array (listaDeObjectos). When you are in "search mode", you should provide data from separate array which must contain only specific items that adopt your search criteria. UISearchBar calls searchBar:textDidChange: method where you have to check every item in your listaDeObjectos array, and if this item is good for search text provided by UISearchBar, add this item to that separate array. Then you call reloadData method which causes all table view methods to be invoked. So idea is to provide data from "search array" if you are search, and from listaDeObjectos if you are not search.

beefon
thanks!!!!!!!!!
Digital Robot
+2  A: 

I don't know the other source, but I think he used his own array for the data. So you should probably do something like

for (NSDictionary *dict in listaDeObjectos)
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    NSString *oneName = [dict objectForKey:@"name"];

    NSRange r = [name rangeOfString:searchText];
    if(r.location != NSNotFound)
        [searchedData addObject:name];
    counter++;
    [pool release];
}

instead.

Eiko
thanks! Your message complemented beefon's (+1) !
Digital Robot