這真的是我的最後手段,因爲我絕對難倒,我只知道這是愚蠢的!Objective-C變化的變量隨機值
我有一個UITableView和一個UISearchBar,用戶使用搜索欄輸入一個位置,然後將該位置附加到page = 1的url。然後發送給api,並返回廣告列表(這已成功)。然後,用戶可以滾動到UITableView的底部並拉動以加載結果的下一頁(頁碼增加並且API再次被調用,也是成功的)。
如果我硬編碼到位置變量的地方「倫敦」的廣告加載儘可能多的網頁罰款,但是當我使用searchBar.text(這似乎是正確的),頁面1加載罰款,但頁面2崩潰/網址無效。當我輸出位置變量時,它不是一個字符串了,因此崩潰或者它是一些隨機數據。
我已經在網上大量搜索,沒有發現任何與一直停留在這2天,任何幫助,將不勝感激:)
我的代碼如下:
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
謝謝,我實際上並沒有得到它一個正確的答案,但我已經接受了在朝着正確的方向最接近的一個:) – treeba 2010-08-19 10:00:40