when app run, titles(label in collectionview) are not appearing. scrolling up or down then titles are appearing. Also in numberOfItemsInSection,[news count] result is 0.
fffdgdfgdfghfghgfhgfhgghghhgjgjghjghjh
- (void)viewDidLoad
{
NSLog(#"viewDidLoad ");
[super viewDidLoad];
leftMenu.target=self.revealViewController;
leftMenu.action=#selector(revealToggle:);
self.title=#"Main";
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
[UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
NSURL *url=[NSURL URLWithString:#"http://www.test.com/ios/GetMainInfoByCat.php?cat_id=76"];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
//self.collectionview.dataSource=self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
data=[[NSMutableData alloc]init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)Thedata
{
[data appendData:Thedata];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"connectionDidFinishLoading ");
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
dnews=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
news=[dnews objectForKey:#"news_main_info_by_cat"];
NSString *str=[news valueForKey:#"img_url"];
//[self.collectionview reloadData];
//NSLog(#"%#",str);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *erroMsg=[[UIAlertView alloc]initWithTitle:#"Error" message:#"Qoşula bilmədi. İnternet bağlantınızı yoxlayın" delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil, nil];
[erroMsg show];
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
[collectionView.collectionViewLayout invalidateLayout];
return 1;
}
-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSLog(#"%i",[news count]);
[collectionView.collectionViewLayout invalidateLayout];
return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *cell=(UICollectionView *)[collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
cell.newsTitle.text=[[news objectAtIndex:indexPath.row] objectForKey:#"post_title"];
//cell.newsImage.image=[NSData dataWithContentsOfURL:[NSURL URLWithString:[[news objectAtIndex:indexPath.row] objectForKey:#"img_url"]]];
//[[news objectAtIndex:indexPath.row] objectForKey:#"img_url"];
return cell;
}
Call reloadData
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"connectionDidFinishLoading ");
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
dnews=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
news=[dnews objectForKey:#"news_main_info_by_cat"];
NSString *str=[news valueForKey:#"img_url"];
[self.collectionview reloadData];
}
Update: make sure IBoutlet is connected to collectionView
--> When you are scrolling collectionview or tableview at that time they are reloading the data so that when you are get your array after that please reload your scroll view this will sure help you.
Related
I have a JSON data from http://vmg.hdvietpro.com/ztv/home . I want to get text1,thumbnailImage value in this JSON after clicking getData button to get text1,thumbnailImage(NSString) values from JSON to table view. This is my code and after click button nothing happen. Thanks for help.
#interface ViewController ()
{
NSMutableData *webData;
NSURLConnection *connection;
NSMutableArray *moviesArray;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[self myTableView]setDelegate:self];
[[self myTableView]setDataSource:self];
moviesArray=[[NSMutableArray alloc]init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[webData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(#"Fail");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSDictionary *allDataDictionary=[NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
NSDictionary *response=[allDataDictionary objectForKey:#"response"];
NSDictionary *hotProgram=[response objectForKey:#"hot_program"];
NSArray *itemPageHot=[hotProgram objectForKey:#"page"];
for (NSDictionary*dic in itemPageHot) {
NSString *text1=[dic objectForKey:#"text1"];
[moviesArray addObject:text1];
}
[[self myTableView]reloadData];
}
- (IBAction)getData:(id)sender {
NSURL *url=[NSURL URLWithString:#"http://vmg.hdvietpro.com/ztv/home"];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
connection = [NSURLConnection connectionWithRequest:request delegate:self];
if(connection){
webData =[[NSMutableData alloc]init];
NSLog(#"ket noi thanh cong");
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [moviesArray count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString*CellIndentifier=#"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIndentifier];
if(!cell){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier];
}
cell.textLabel.text=[moviesArray objectAtIndex:indexPath.row];
return cell;
}
You're not traversing the dictionary correctly. The key page is a child of key itemPage so this should work
NSArray *itemPageHot = [[hotProgram objectForKey:#"itemPage"] objectForKey:#"page"];
I'd recommend downloading a JSON inspector for issues like this. VisualJSON in the app store is what I use.
I try to get the Images from a JSON URL and place on UICollectionView
but i don't how to get images from JSON
#import "ViewController.h"
#import "CustomCell.h"
#interface ViewController ()
{
NSArray *arrayOfImages;
NSArray *arrayOfDescriptions;
NSMutableArray *json;
NSString *img;
NSMutableData *webData;
NSURLConnection *connection;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
/**/
[[self myCollectionView]setDataSource:self];
[[self myCollectionView]setDelegate:self];
NSString *urlString=[NSString stringWithFormat:#"http://ielmo.xtreemhost.com/array.php"];
NSURL * url=[NSURL URLWithString:urlString];
NSURLRequest *req=[NSURLRequest requestWithURL:url];
connection=[NSURLConnection connectionWithRequest:req delegate:self];
if(connection)
{
NSLog(#"Connected");
webData=[[NSMutableData alloc]init];
}
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"Error is");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSMutableArray *al=[NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
NSLog(#"all array isn %#",al);
NSData* myData = [NSKeyedArchiver archivedDataWithRootObject:al];
NSLog(#"Data is data%#",myData);
NSError *error;
json=(NSMutableArray*)[NSJSONSerialization JSONObjectWithData:myData options:kNilOptions error:&error];
NSLog(#"Data is arrayjson%#",json);
for(int i=0;i<json.count;i++)
{
img=[NSString stringWithFormat:#"%#",[json objectAtIndex:i]];
NSLog(#"Data is arrayimage%#",img);
}
NSURL *urlOne=[NSURL URLWithString:img];
NSLog(#"Data is arrayurl%#",urlOne);
NSData *newData=[NSData dataWithContentsOfURL:urlOne];
UIImageView *imaegView=[[UIImageView alloc]initWithFrame:CGRectMake(38,0, 76, 96)];
[imaegView setImage:[UIImage imageWithData:newData]];
[self.myCollectionView addSubview:imaegView];
[[self myCollectionView]reloadData];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [arrayOfImages count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier=#"Cell";
CustomCell *cell=[ collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
[[cell myImage]setImage:[UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]]];
// [[cell myLabel]setText:[arrayOfDescriptions objectAtIndex:indexPath.item]];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
i tried this code but in my output image are not display in my collectionView
please tell me how to retrieve images from json to My-collection VIew
As your JSON contains Image.
It must be encoded while transmitting mostly in Base64. Now you need to decode it to form NSData.
Once you have converted it to NSData then you can easily form an UIImage from it, after this you can show the image on collection view.
I want to create an App which parses the picture Links of a Facebook page and shows them in an iOS app (display them in tableView). But after I parsed the JSON file and added them to an array which should be downloaded nothing is displayed.
Here's the code:
- (void)viewDidLoad
{
self.edgesForExtendedLayout = UIRectEdgeNone;
[super viewDidLoad];
items = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:#"https://graph.facebook.com/HuppFotografie/photos/uploaded/?fields=id,name,picture"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
connection = [NSURLConnection connectionWithRequest:request delegate:self];
if (connection) {
webData = [[NSMutableData alloc]init];
}
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"fail with error");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
NSArray *arrayOfData = [allDataDictionary objectForKey:#"data"];
for (NSDictionary *dicton in arrayOfData) {
NSString *picture = [dicton objectForKey:#"picture"];
[items addObject:picture];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 250.0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [items count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"MyImageCell";
ImageCell *cell = (ImageCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor clearColor];
if (cell == nil) {
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ImageCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (ImageCell *)currentObject;
break;
}
}
}
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:[items objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:#"Placeholder.jpg"]];
cell.imageSource.text = [items objectAtIndex:indexPath.row];
return cell;
}
I really don't get why. I'm sorry if this question is stupid but I am coding just as a hobby and have not that great experience.
In fact your connection is asynchronous, so you just need to reload your table view programmatically to display loaded images:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
NSArray *arrayOfData = [allDataDictionary objectForKey:#"data"];
for (NSDictionary *dicton in arrayOfData) {
NSString *picture = [dicton objectForKey:#"picture"];
[items addObject:picture];
}
// Just add this line, in order to force reload when all your data is arrived
[self.tableView reloadData];
}
I have json like this (have dictionary and array).
I want draw all json into UItableviewcell, but its not draw there
{ data: [
{
featured: true,
price: {
currency: "IDR",
amount: 5557679,
formatted: "Rp5.558.000"
},
] }
and this is my viewcontroller.h file
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController{
IBOutlet UITableView *mainTableView;
NSURL *URL;
NSDictionary *Deals;
NSArray *Deals_array;
NSMutableData *data;
}
#end
and this is my .m file
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title=#"SOme Apps";
[UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
URL = [NSURL URLWithString:[NSString stringWithFormat:#"http://someurl.com"]];
NSURLRequest *request=[NSURLRequest requestWithURL:URL];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
/*dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL: URL];
[self performSelectorOnMainThread:#selector(fetchedData:) withObject:data waitUntilDone:YES];
});*/
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
data=[[NSMutableData alloc] init];
NSLog(#"%#",data);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
[data appendData:theData];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSError* error;
Deals= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
Deals_array = [Deals objectForKey:#"data"]; //2
NSDictionary* loan = [Deals_array objectAtIndex:0];
NSString *test=[loan objectForKey:#"featured"];
NSLog(#"%#",test);
[mainTableView reloadData];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(#"Error");
}
////set minimum section oftableview
-(int)numberOfTableviewSection : (UITableView *) tableView
{
return 1;
}
////set length uitableview
-(int) tableView :(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [Deals_array count];
}
///declaring uitableview
-(UITableViewCell *)tableView :(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:#"MainCell"];
if (cell==nil){
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MainCell"];
}
cell.textLabel.text=[[Deals_array objectAtIndex:indexPath.row] objectForKey:#"featured"];
return cell;
}
#end
but its not draw the objectkey #test, can someone help me why?
Try doing the allocation
Deals_array = [[NSArray alloc] initWithArray:[Deals objectForKey:#"data"]];
Here is ALL my code for this specific view controller. As the title of this question states, I am seeing my UITableView loaded with the same 9 values over and over again. If I place a breakpoint in connectionDidFinishLoading and wait one second I only see the data rendered to the table view once. I have tried all sorts of things to solve this; help is very much appreciated.
#implementation MasterViewController
#synthesize response;
NSMutableData* receivedData;
NSString* hostName;
NSInteger portNumber = 9999;
NSMutableDictionary* dictionary;
NSInteger maxRetryCount = 5;
int count = 0;
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(#"Succeeded! Received %d bytes of data",[data length]);
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(#"Connection failed! Error - %# %#",[error localizedDescription],[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
receivedData = nil;
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection {
NSError *error = nil;
id result = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&error];
if ([result isKindOfClass:[NSArray class]]) {
for (NSArray *item in result) {
NSArray *category = [item valueForKey:#"CategoryName"];
[dataArray addObject:category];
}
}
else {
NSDictionary *jsonDictionary = (NSDictionary *)result;
for(NSDictionary *item in jsonDictionary)
NSLog(#"Item: %#", item);
}
connection = nil;
[self.tableView reloadData];
NSLog(#"Finished");
}
-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return YES;
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
}
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection
{
return YES;
}
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSString* username = #"test";
NSString* password = #"testPwd";
NSLog(#"Attempting connection with username: %#", username);
NSMutableString *loginString = (NSMutableString*)[#"" stringByAppendingFormat:#"%#:%#", username, password];
NSString *authHeader = [#"Basic " stringByAppendingFormat:#"%#", loginString];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#:%i%#", hostName, portNumber, #"/api/categories"]];
NSLog(#"URL: %#", url);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url
cachePolicy: NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval: 1.0];
[request addValue:authHeader forHTTPHeaderField:#"Authorization"];
[NSURLConnection connectionWithRequest:request delegate:self];
if ([challenge previousFailureCount] <= maxRetryCount) {
NSURLCredential *credential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
} else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
NSLog(#"Failure count %d",[challenge previousFailureCount]);
}
}
- (void)awakeFromNib
{
[super awakeFromNib];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addNewItem)];
self.navigationItem.rightBarButtonItem = addButton;
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
- (void)addNewItem
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Product"
message:#"Enter the new product name"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)insertNewObject:(id)sender
{
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataArray count];;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[dataArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert)
{
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *object = [dataArray objectAtIndex:indexPath.row];
[[segue destinationViewController] setDetailItem:object];
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
UITextField *textField = [alertView textFieldAtIndex:0];
[dataArray addObject:textField.text];
[self.tableView reloadData];
NSLog(#"Plain text input: %#",textField.text);
NSLog(#"Alert View dismissed with button at index %d",buttonIndex);
}
- (void)viewWillAppear:(BOOL)animated {
self.title = #"Categories";
receivedData = [[NSMutableData alloc] init];
hostName = [[NSString alloc] initWithString:#"12.234.56.123"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#:%i%#", hostName, portNumber, #"/api/categories"]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 1.0];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
if (connection) {
} else {
}
dataArray = [[NSMutableArray alloc] init];
}
#end
willSendRequestForAuthenticaiton is a delegate method. Yet inside your delegate method, you are generating a brand new URL request, which in turn gets handled by the willSendRequest delegate, which generates another request, etc, which continually populates your dataArray with data.
Get rid of the url request inside the delegate method and it should fix things for you.