Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-21-2011, 04:06 PM   #1 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 8
wright0768 is on a distinguished road
Default Data not showing up in tableview

Ok so I am thinking this might be a problem in IB but I figured I would post the code and at the very least it might help someone else out doing an asynchronous request or using NSDictionary

ViewController.h
Code:
#import <UIKit/UIKit.h>
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "AppDelegate.h"

@interface ViewController : UIViewController
{
    NSMutableArray  *photoTitles;
    
}
@property (nonatomic, strong) NSMutableArray *photoTitles;

@end
ViewController.m
Code:
#import "ViewController.h"
#import "SBJson.h"

@implementation ViewController
@synthesize photoTitles;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView
 {
 }
 */


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    // [self setupArray];
    [super viewDidLoad];
    photoTitles = [[NSMutableArray alloc] init];
    NSURL *url = [NSURL URLWithString:@"http://localhost/test.php"];
	ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setDelegate:self];
    [request startAsynchronous];
    
}
- (void)requestFinished:(ASIFormDataRequest *)request
{
    
    NSString *responseString = [request responseString];
    
    NSDictionary *dictionary = [responseString JSONValue]; 
    NSArray *photos = [[dictionary objectForKey:@"photos"] objectForKey:@"photo"];
    
    for (NSDictionary *photo in photos) {
        NSString *title = [photo objectForKey:@"title"];
        [photoTitles addObject:title];
        
        
    }    
   
    
    // Use when fetching text data

    // Use when fetching binary data
    NSData *responseData = [request responseData];
    NSLog(@"Data: %@", responseData);
    
    

    
    
}
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
	return [photoTitles count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView
		cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
							 SimpleTableIdentifier];
	if(cell == nil){
		cell = [[UITableViewCell alloc] initWithFrame:CGRectZero
									   reuseIdentifier: SimpleTableIdentifier];
	}
	NSUInteger row = [indexPath row];
	cell.textLabel.text = [photoTitles objectAtIndex:row];

	return cell;
        
}


- (void)requestFailed:(ASIFormDataRequest *)request
{
    NSError *error = [request error];
    NSLog(@"error: %@", error);
    
}






- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
This does not have any errors except the data from the photoTitles mutable array does not show up in the table view. Any help would be great.
wright0768 is offline   Reply With Quote
Old 12-21-2011, 04:16 PM   #2 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 8
wright0768 is on a distinguished road
Default

Here is a more simplified version of ViewController.m

Code:
- (void)viewDidLoad
{

    [super viewDidLoad];
    photoTitles = [[NSMutableArray alloc] init];
    NSURL *url = [NSURL URLWithString:@"http://localhost/test.php"];
	ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setDelegate:self];
    [request startAsynchronous];
    
}
- (void)requestFinished:(ASIFormDataRequest *)request
{
    
    NSString *responseString = [request responseString];
    
    NSDictionary *dictionary = [responseString JSONValue]; 
    NSArray *photos = [[dictionary objectForKey:@"photos"] objectForKey:@"photo"];
    
    for (NSDictionary *photo in photos) {
        NSString *title = [photo objectForKey:@"title"];
        [photoTitles addObject:title];
        
        
    } 
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
	return [photoTitles count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView
		cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
							 SimpleTableIdentifier];
	if(cell == nil){
		cell = [[UITableViewCell alloc] initWithFrame:CGRectZero
									   reuseIdentifier: SimpleTableIdentifier];
	}
	NSUInteger row = [indexPath row];
	cell.textLabel.text = [photoTitles objectAtIndex:row];

	return cell;
wright0768 is offline   Reply With Quote
Old 12-21-2011, 04:28 PM   #3 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

You aren't reloading the table.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 12-21-2011, 04:42 PM   #4 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 8
wright0768 is on a distinguished road
Default

I tried reloading the table before with [tableView reloadData]; however it still doesn't work. I might have it in the wrong place.


Quote:
Originally Posted by BrianSlick View Post
You aren't reloading the table.
wright0768 is offline   Reply With Quote
Old 12-21-2011, 04:43 PM   #5 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Well, it certainly isn't going to work without it. And unless you feel like sharing where you were trying it, I can only speculate.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 12-21-2011, 04:51 PM   #6 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 8
wright0768 is on a distinguished road
Default

Sorry about that, I put [tableView reloadData]; right after the for loop.
wright0768 is offline   Reply With Quote
Old 12-21-2011, 04:54 PM   #7 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

You don't seem to have a table view property declared anywhere. Are you sure you aren't getting errors?
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 12-21-2011, 05:04 PM   #8 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 8
wright0768 is on a distinguished road
Default

Thanks so much Brian, that put me in the right direction and fixed it. The problem was just leaving out IBOutlet in @property (nonatomic, retain) UITableView *myTableView;

When I make mistakes like this it tells me I need to walk away for a few minutes.

Thanks again.



Quote:
Originally Posted by BrianSlick View Post
You don't seem to have a table view property declared anywhere. Are you sure you aren't getting errors?
wright0768 is offline   Reply With Quote
Reply

Bookmarks

Tags
asynchronous, nsarray, nsdictionary, tableview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 392
13 members and 379 guests
7twenty7, AppsBlogger, Creativ, Dalia, David-T, Duncan C, HemiMG, heshiming, LunarMoon, Murphy, pbart, teebee74, Tomsky
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,127
Posts: 402,915
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:50 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0