2010-08-19 8 views
0

C'est vraiment mon dernier recours car je suis absolument perplexe et je sais juste que c'est quelque chose de stupide!Objective-C changer les valeurs aléatoires pour les variables

J'ai un UITableView et un UISearchBar, l'utilisateur utilise la barre de recherche pour entrer un emplacement, qui est ensuite ajouté à une URL avec page = 1. Celui-ci est ensuite envoyé à un api et une liste d'annonces est renvoyée (cela a été un succès). L'utilisateur peut alors faire défiler vers le bas de l'UITableView et tirer pour charger la page suivante des résultats (le numéro de page est incrémenté et l'API est appelée à nouveau, également avec succès).

Si je code dur dans la variable d'emplacement le lieu "London" les annonces se chargent bien pour autant de pages que possible, mais quand j'utilise le searchBar.text (qui semble être correct), la page 1 se charge bien mais la page 2 plantages/url invalides. Lorsque je publie la variable d'emplacement, elle n'est plus une chaîne et donc des plantages ou des données aléatoires.

J'ai longuement cherché en ligne et n'a rien trouvé et ont été coincés sur ce pendant 2 jours, toute aide serait grandement appréciée :)

Mon code est le suivant:

PropertySearchTableViewController.h

#import <UIKit/UIKit.h> 
#import <QuartzCore/QuartzCore.h> 

@interface PropertySearchTableViewController : UITableViewController <UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource> { 

    IBOutlet UITableView *propertiesTableView; 
    UISearchBar *propertiesTableSearch; 
    NSMutableArray *propertiesArray; 

    NSString *location; 
} 

@property (nonatomic, retain) IBOutlet UISearchBar *propertiesTableSearch; 
@property (nonatomic, retain) NSMutableArray *propertiesArray; 
@property (nonatomic, retain) NSString *location; 

- (void)loadProperties; 
- (void)callback:(NSDictionary *)response; 

@end 

PropertySearchTableViewController.m

#import "PropertySearchTableViewController.h" 
#import "JSONHandler.h" 

@implementation PropertySearchTableViewController 
@synthesize location; 
@synthesize propertiesArray; 
@synthesize propertiesTableSearch; 

int page = 1; 

#pragma mark - 
#pragma mark View lifecycle 

- (void)viewDidLoad { 
    location = @"London"; 
    [self.propertiesTableSearch becomeFirstResponder]; 
    self.propertiesArray = [[NSMutableArray alloc] init]; 
    [self loadProperties]; 
    self.title = @"Properties"; 
} 

- (void)loadProperties { 
    NSLog(@"Calling: %@", ([location isKindOfClass:[NSString class]] ? location : @"No longer a string")); 
    NSString *url = [NSString stringWithFormat:@"http://local.somewebsite.co.uk/adverts/?where=%@&page=%i", location, page]; 
    JSONHandler *handler = [[JSONHandler alloc] initWithUrl:url andData:nil callbackObject:self]; 
    [handler handleConnection]; 
} 

- (void)callback:(NSDictionary *)response { 
    NSArray *properties = [response objectForKey:@"results"]; 
    NSEnumerator *enumerator = [properties objectEnumerator]; 
    NSDictionary* item; 
    while (item = (NSDictionary*)[enumerator nextObject]) { 
     NSDictionary *d = item; 
     [propertiesArray addObject:d]; 
    } 
    [self.tableView reloadData]; 
} 

#pragma mark - 
#pragma mark Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [self.propertiesArray 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]; 
    } 
    NSDictionary *d = [propertiesArray objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [d objectForKey:@"ad_title"]; 
    return cell; 
} 

#pragma mark - 
#pragma mark Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
} 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ 
    CGPoint p = scrollView.contentOffset; 
    CGSize s = scrollView.contentSize; 
    CGSize scrollSize = scrollView.bounds.size; 
    if((p.y+scrollSize.height) > (s.height+50)){ 
     self.tableView.contentSize = CGSizeMake(0, s.height+50); 
     page++; 
     [self loadProperties]; 
    } 
} 

//Search 

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { 
    [searchBar setShowsCancelButton:YES animated:YES]; 
    self.tableView.scrollEnabled = NO; 
} 

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { 
    [email protected]""; 
    [searchBar setShowsCancelButton:NO animated:YES]; 
    [searchBar resignFirstResponder]; 
    self.tableView.scrollEnabled = YES; 
} 

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
    location = (NSString *) searchBar.text; 
    page = 1; 
    NSLog(@"%@", searchBar.text); 
    [propertiesArray removeAllObjects]; 
    [self loadProperties]; 
    [self.tableView reloadData]; 
    [searchBar setShowsCancelButton:NO animated:YES]; 
    [searchBar resignFirstResponder]; 
    self.tableView.scrollEnabled = YES; 
} 

#pragma mark - 
#pragma mark Memory management 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Relinquish ownership any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand. 
    // For example: self.myOutlet = nil; 
} 


- (void)dealloc { 
    [location release]; 
    [propertiesTableSearch release]; 
    [super dealloc]; 
} 


@end 
+0

Merci, je n'ai pas fait une bonne réponse pour elle, mais j'ai accepté le plus proche qui se dirigeait dans la bonne direction :) – treeba

Répondre

0

Cette ligne est probablement le coupable:

location = (NSString *) searchBar.text; 

Vous devez conserver/copier la chaîne ou il disparaîtra!

[location release]; //!< Release the last location string 
[location = [[searchBar text] copy]; //!< Get a copy of the new one 
+0

ne peux vous dire combien je suis reconnaissant! Travaillé un charme :) – treeba

+0

Dès que vous avez dit «n'est plus une chaîne plus» il allait certainement être une retenue manquante;) – deanWombourne

+0

Voilà ce que je pensais mais je ne pouvais pas trouver nulle part (relativement nouveau à cet ensemble » chose de la gestion de la mémoire) merci encore! – treeba

Questions connexes