Reputation: 1193
I have a UITableView that I would like to populate with data via a WebAPI. I have everything set up and I can verify that data is returned in JSON format. What I don't understand is why I see that data in my array printed out several times when I use NSLog and also how do I assign this data to the UITableView. I was able to populate the table from the viewDidLoad method but these are hard-coded values. I want to populate the grid with data that is returned from my call to the remote server. This data is available to me in my didReceiveData delegate. What am I doing wrong here?
I have this code in my MasterViewControler.h
@interface MasterViewController : UITableViewController <UITableViewDataSource, UIAlertViewDelegate> {
NSMutableArray *dataArray;
NSMutableArray *categoryNames;
}
Here is an abridged version of what I have in my MasterViewController.m file
NSMutableData* receivedData;
NSString* hostName;
NSInteger portNumber = 9999;
NSMutableDictionary* dictionary;
NSInteger maxRetryCount = 5;
int count = 0;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"Succeeded! Received %d bytes of data",[data length]);
NSError *error = nil;
// Get the JSON data from the website
id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if ([result isKindOfClass:[NSArray class]]) {
for (NSArray *item in result)
[dataArray addObject:item];
NSLog(@"%@", dataArray);
}
else {
NSDictionary *jsonDictionary = (NSDictionary *)result;
for(NSDictionary *item in jsonDictionary)
NSLog(@"Item: %@", item);
}}
- (void)viewDidLoad{
[super viewDidLoad];
hostName = [[NSString alloc] initWithString:@"12.34.56.78"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%i%@", hostName, portNumber, @"/api/products"]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 2];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
[connection start];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewItem)]; //insertNewObject
self.navigationItem.rightBarButtonItem = addButton;
dataArray = [[NSMutableArray alloc] init];
//[dataArray addObject:@"Apple"];
//[dataArray addObject:@"Mango"];
//[dataArray addObject:@"Orange"];}
Instead of populating the dataArray here I was trying to populate it in the didReceiveData delegate. The dataArray will be allocated but it is almost like I have to reload the UITableView to see the values. I tried that at the end of didReceiveData but I received an error.
Upvotes: 0
Views: 876
Reputation: 2501
First make sure your UITableViewDataSource
and UITableViewDelegate
are set.
Once the array is populated (in your case, at the end of -(void)connection:didReceiveData:
) you'd call [tableView reloadData]
to refresh the table.
Then you'd set your cell in:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// If your array is an array of strings, change if applicable
NSString *string = [dataArray objectAtIndex:indexPath.row];
[cell.textLabel setText:string];
}
Upvotes: 2
Reputation: 18290
Your class should implement UITableView's UITableViewDataSource protocol, and then assign itself as the dataSource. Then implement cellForRowAtIndexPath:, which is where you actually use your model array to initialize the cells. When your data is ready, call reloadData on the table view.
Upvotes: 0