Question

I'm having some trouble with displaying results from a datasource. This code will show a different (and correct) result in the console, but results in all kinds of random crap in the simulator.

("results" is an NSMutableArray property for the class.)

-(void) handleSearchForKeywords: (NSString *) keywords {
    [results removeAllObjects];
    int r = rand() % 10;
    for( int i = 0; i < r; i++ ) {
        [results addObject:[NSString stringWithFormat:@"test %i: %@", i, keywords]];
    }
    [self reloadTheTable];
}

-(void) reloadTheTable {
    NSLog( @"current array contents: %@", results );
    [tableView reloadData];
}

I'm guessing that this might have something to do with memory retention of the array, or the strings in the array? I'm afraid I still haven't got the hang of that.

[edit in response to Marc Bessey -- I think everything here is your basic datasource methods]

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

-(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath {
    static NSString *SearchViewControllerCell = @"SearchViewControllerCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SearchViewControllerCell];
    if( cell == nil ) {
        cell = [[[UITableViewCell alloc] initWithFrame: CGRectZero reuseIdentifier: SearchViewControllerCell] autorelease];
        NSUInteger row = [indexPath row];
        [cell setText:[results objectAtIndex:row]];
    }
    return cell;
}
Was it helpful?

Solution

I don't think the problem is in the code that you've posted. It's more likely in the code that implements the datasource for your table view.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top