2010-08-19 101 views
0

这真的是我的最后手段,因为我绝对难倒,我只知道这是愚蠢的!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 
+0

谢谢,我实际上并没有得到它一个正确的答案,但我已经接受了在朝着正确的方向最接近的一个:) – treeba 2010-08-19 10:00:40

回答

0

此行可能是罪魁祸首:

location = (NSString *) searchBar.text; 

您需要保留/复制字符串或将消失!

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

不能告诉你我有多么感激!工作的魅力:) – treeba 2010-08-19 10:18:32

+0

只要你说'不再是一个字符串'它肯定会失去一个保留;) – deanWombourne 2010-08-19 10:24:19

+0

这就是我的想法,但我找不到它在任何地方(相对较新的整体'内存管理“的东西)再次感谢! – treeba 2010-08-19 10:26:33