views:

212

answers:

3

I'm loading data dynamically from an API using a UISearchBar and trying to display it, using something like this:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    NSLog(@"Search Text: %@",[searchBar text]);
    [self startSearchingWithText:[searchBar text]];
}
- (void) startSearchingWithText:(NSString *)text {
    ...
    // Go through the list of services and set up a query for the search term
    QueryServiceManager *manager = [[QueryServiceManager alloc] initWithServices: services];

    // Set up the query
    if (![manager addKeyword: text])
        NSLog(@"PROBLEM!");

// Execute the query and store the titles in an array
self.data = [[manager makeQuery] copy]; // Returns a NSArray

NSLog(@"%@",self.data);

// Add the items to the tableView

[self.tableView reloadData];

My UITableViewController code is set up to read from self.data, which is initially a NSArray of @"" elements, as such:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
[self.data count];
}
- (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] autorelease];
}

// Set up the cell...

cell.textLabel.text = [self.data objectAtIndex:indexPath.row];

return cell;
}

All I get when I do this is:

(gdb) continue 2010-02-26 18:43:32.515 iSave[11690:207] ( "Toshiba Satellite L505-GS5037 TruBrite 15.6-Inch Laptop (Black)", "Dell Inspiron 11 11.6-Inch Obsidian Black Laptop (Windows 7 Premium)", "ASUS Eee PC Seashell 1005PE-MU17-BK 10.1-Inch Black Netbook - Up to 11 Hours of Battery Life", "SwissGear Computer Backpack (Red)", "Belkin 36-Piece Demagnatized Computer Tool Kit with Case (Black)", "Compaq EVO N610C", "Belkin F8E062 55-Piece Computer Tool Kit with Black Case (Demagnetized Tools)", "ASUS Eee PC Seashell 1005PE-PU17-BK 10.1-Inch Black Netbook - Up to 14 Hours of Battery Life", "Harman Kardon SoundSticks II 2.1 Plug and Play Multimedia Speaker System", "ION Audio VCR 2 PC USB VHS Video to Computer Converter" ) (gdb) continue Program received signal: “EXC_BAD_ACCESS”. (gdb)

Any ideas? It looks like the NSArray is being populated properly, then things are failing on/after the reloadData call (breakpoints confirm this, but can't isolate where things are going wrong)

EDIT: I replaced the NSLog() with enumeration (for ( NSString *elem in self.data ) NSLog(elem);), and it still crashes. I managed to coax a log out of it: http://pastebin.com/NDVKLsJC

When I remove the NSLog() entirely, it doesn't crash, but the TableView doesn't update, either.

+1  A: 

Something weird: You're saying that you only have 1 row in each section, yet you're using indexPath.row to index into your array? One of those is wrong, and I'm guessing it's the first. Usually if you're displaying an array (of n elements) in a UITableView, you have one section and n rows in that section, which means you'd use [myArray objectAtIndex:indexPath.row] to retrieve the appropriate object.

If you want a separate section for each item in the array, then you say that you have n sections, and 1 row in each section, and then use [myArray objectAtIndex:indexPath.section] to retrieve the appropriate object.

Oh, and you're leaking memory all over the place.

Dave DeLong
Like the person below, I just have "return 1;" in there for testing purposes. I'll edit the code above to reflect that, and put the correct code ([self.data count]) in, but it's the same problem regardless.
Jason B
A: 

What about:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.data count];
}

instead of always returning 1.

St3fan
That's not the problem; I have it returning 1 so that I can make sure it works without going through the whole process. If it works for 1, it'll be easier to get it to work for the whole array. I just edited the code to reflect the "proper" counting, but the same problem arises.
Jason B
A: 

Have you identified the object which you're trying to access but which no longer exists, the object which is giving EXC_BAD_ACCESS?

If not then you should enable Zombies in your app, then you can issue a command to gdb which will tell you which object is causing the crash.

Griffo