I have a page that contains a tableview – and a textfield and button that sends a zip code to a PHP page and returns JSON data. I am not able to populate the table view with the returned data. The NSLog shows the data is being returned, but the table remains blank. No errors.
Code:
- (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;
}
Header File:
@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
I believe the basic issue is that you are trying to build a composite view (textfield, button and tableview) and use a
UITableViewControlleras the controller. It’s been cited here on SO and elsewhere that this is basically a no-go. Instead, try something like:then wire the
UITableViewin IB to thetableViewivar in the File’s Owner — it appears from your comments about that you were attempting to wire the table view element in the composite view to the File’s Owner, aUIViewController— not what you want.