Objective-C改变变量的随机值

时间:2010-08-19 09:48:00

标签: iphone objective-c xcode variables global-variables

这真的是我的最后一招,因为我绝对难倒,我只知道这是愚蠢的事情!

我有一个UITableView和一个UISearchBar,用户使用搜索栏输入一个位置,然后将其附加到一个页面= 1的网址。然后将其发送到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 {
    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 anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


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


@end

1 个答案:

答案 0 :(得分:0)

这条线可能是罪魁祸首:

location = (NSString *) searchBar.text;

你需要保留/复制字符串,否则它就会消失!

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