savagenoob
savagenoob

Reputation: 413

IPhone TableView not Populating JSON Data

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

Upvotes: 0

Views: 571

Answers (3)

jbrennan
jbrennan

Reputation: 12003

You should be using cell.textLabel.text to set the text of your table cell. The text property is deprecated.

Also, try setting a breakpoint on that line to make sure the local variables you have there actually have the values you'd expect (i.e. the correct JSON values). Maybe you have a typo in one of your key strings (like the casing)?

Upvotes: 0

jstevenco
jstevenco

Reputation: 2953

I believe the basic issue is that you are trying to build a composite view (textfield, button and tableview) and use a UITableViewController as the controller. It's been cited here on SO and elsewhere that this is basically a no-go. Instead, try something like:

@interface SearchZipViewController : UIViewController<UITableViewDelegate, UITableViewDataSource> {
    IBOutlet UIButton* searchButton;
    IBOutlet UITextField* zipField;
    IBOutlet UITableView* tableView;
}

@end

then wire the UITableView in IB to the tableView ivar 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, a UIViewController -- not what you want.

Upvotes: 2

sosborn
sosborn

Reputation: 14694

At the end of your search zip method you should add this line:

[tableView reloadData];

Otherwise your tableView has no way of knowing that you have new data to show.

Upvotes: 0

Related Questions