开发者

EXC_BAD_ACCESS on ReloadData

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 theNSLog() 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 UITableViewdoesn't update, either.


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.


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.


What about:

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

instead of always returning 1.


Hit shift-command-b to analyze the project and solve all problems displayed (potential leaks, etc.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜