This really is my last resort as I am absolutely stumped and I just know it is something stupid!
I have a UITableView and a UISearchBar, the user uses the search bar to enter a location, which is then appended to a url with page=1. This is then sent to an api and a list of adverts are returned (this has been successful). The user can then scroll to the bottom of the UITableView and pull to load the next page of results (the page number is incremented and the api is called again, also successful).
If I hard code into the location variable the place "London" the adverts load fine for as many pages as possible, however when I use the searchBar.text (which seems to be correct), page 1 loads fine but page 2 crashes/url invalid. When I output the location variable it either isn't a string anymore and therefor crashes or it is some random data.
I have extensively searched online and found nothing and have been stuck on this for 2 days, any help would be greatly appreciated :)
My code is as follows:
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 {
searchBar.text=@"";
[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 anythin开发者_高级运维g that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc {
[location release];
[propertiesTableSearch release];
[super dealloc];
}
@end
This line's probably the culprit :
location = (NSString *) searchBar.text;
You need to retain/copy the string or it will go away!
[location release]; //!< Release the last location string
[location = [[searchBar text] copy]; //!< Get a copy of the new one
精彩评论