【发布时间】:2011-12-28 00:50:10
【问题描述】:
我有一个页面,其中包含一个 tableview - 以及一个将邮政编码发送到 PHP 页面并返回 JSON 数据的文本字段和按钮。我无法用返回的数据填充表格视图。 NSLog 显示正在返回数据,但该表仍为空白。没有错误。 代码:
- (IBAction) searchzip: (id) sender
{
NSString *post = [NSString stringWithFormat:@"zip=%@", zipField.text];
NSString *hostString = @"https://www.mysite.com/searchzip.php?";
// Append string and add percent escapes
hostString = [[hostString stringByAppendingString:post] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *hostURL = [NSURL URLWithString:hostString];
NSString *jsonString = [[NSString alloc] initWithContentsOfURL:hostURL];
self.zipArray = [jsonString JSONValue];
NSLog(@"%@", zipArray);
[jsonString release];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [zipArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
NSDictionary *infoDictionary = [self.zipArray objectAtIndex:indexPath.row];
static NSString *Prospects = @"agencyname";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Prospects];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:Prospects] autorelease];
}
// setting the text
cell.text = [infoDictionary objectForKey:@"agencyname"];
self.navigationItem.title = @"Zip Search";
// Set up the cell
return cell;
}
头文件:
@interface SearchZipViewController : UITableViewController{
NSMutableArray *zipArray;
IBOutlet UITextField *zipField;
IBOutlet UIButton *searchButton;
}
@property (nonatomic, retain) NSMutableArray *zipArray;
@property (nonatomic, retain) UITextField *zipField;
@property (nonatomic, retain) UIButton *searchButton;
- (IBAction) searchzip: (id) sender;
@end
【问题讨论】:
标签: objective-c cocoa-touch iphone-sdk-3.0