Προς το περιεχόμενο

JSON σε i phone (objective c)


jimmysnn_17

Προτεινόμενες αναρτήσεις

Δημοσ.

Καλησπέρα, πως μπορώ να μοντελοποιήσω το αρχείο Json που λαμβάνω και να να βάλω κάποια απο τα δεδομένα στο TableView. Αν υπάρχει κάποιο tutorial η κάτι τέτοιο!!!

ο κωδικας μου

 

>#import <UIKit/UIKit.h>
@class SpotlightClass;

@interface EventViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *listOfEvents;
NSMutableArray *Spotlight;
NSMutableArray *news;
NSMutableArray *popular;
IBOutlet UITableView *dummyTable; 

IBOutlet UISegmentedControl *myMent;

NSMutableData *dataWebService;

	}


@property (nonatomic,retain) IBOutlet UITableView *dummyTable;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSArray *paymentWays;
-(IBAction)segmentAction:(id)sender;


@end

 

και το .m

 

>#import "EventViewController.h"
#import "EventsDetailViewController.h"
#import "EventTicketsViewController.h"
#import "EventInfoViewController.h"
#import "NewsController.h"
#import "JSON.h"
#import "SpotlightClass.h"
#import "SpotlightLocation.h"
#import "PaymentWay.h"
#import "TicketType.h"





@implementation EventViewController
@synthesize dummyTable,name,paymentWays;

@class SpotlightClass;


- (IBAction) segmentAction:(id)sender

{

   UISegmentedControl* segCtl = sender ;
   if( [segCtl selectedSegmentIndex] == 0 )
   {
       NSLog(@"hi this is first segment");
	//[ self.view addSubview: first] ;// to add view use this statement first is the view controller name
	listOfEvents = Spotlight;
   }
   if( [segCtl selectedSegmentIndex] == 1 )
   {
       NSLog(@"hi this is second segment");
	listOfEvents = news;
	//[ self.view addSubview:second] ;
   }
if( [segCtl selectedSegmentIndex] == 2 )
   {
       NSLog(@"hi this is third segment");
	//    [ self.view addSubview:third] ;
	listOfEvents = popular;
   }

NSLog(@"%@",listOfEvents);
[dummyTable reloadData];
}


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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
static NSString *CellIdentifier = @"Cell";
	
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
	cell = [[[uITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
	
// Set up the cell...
NSString *cellValue = [listOfEvents objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

	return cell;		
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Initialize the detail view controller and display it.
EventsDetailViewController *eventdetailController = [[EventsDetailViewController alloc] initWithNibName:@"EventsDetailView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:eventdetailController animated:YES];
[eventdetailController release];
eventdetailController = nil;

}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
	
	[self tableView:tableView didSelectRowAtIndexPath:indexPath];		
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
   [super viewDidLoad];

Spotlight = [[NSMutableArray alloc] initWithObjects:@"A",@"B",@"C",nil];
news = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",nil];
popular = [[NSMutableArray alloc] initWithObjects:@"A1",@"A2",@"A3",@"A4",@"A5",nil];
 
/*
Spotlight = [[NSMutableArray alloc] initWithObjects: nil];
news = [[NSMutableArray alloc] initWithObjects: nil];
popular = [[NSMutableArray alloc] initWithObjects: nil];
*/
dummyTable = [[NSMutableArray alloc] init];

dummyTable = [[uITableView alloc] initWithFrame:CGRectMake(0,30,320,400) style:UITableViewStylePlain];
dummyTable.delegate = self;
dummyTable.dataSource = self;

listOfEvents=Spotlight; 
[self.view addSubview:dummyTable];
dummyTable.scrollEnabled=YES;

[myMent addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
myMent.selectedSegmentIndex = 0 ;


//Set the title
self.navigationItem.title = @"Events";


dataWebService = [[NSMutableData data] retain];
   
   NSMutableURLRequest *request = [[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.2.11/eventora/en/mobile/startjson"]] retain];
   
   NSURLConnection *myConnection = [NSURLConnection connectionWithRequest:request delegate:self];
   
   [myConnection start];  

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
   [dataWebService setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
   [dataWebService appendData:data];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
   
   NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];
   NSLog(@"Response: %@",responseString);

listOfEvents = [responseString  JSONValue]; 
NSLog(@"%@", listOfEvents);

   [responseString release];
   [dataWebService release];
//[dummyTable reloadData];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
   NSLog(@"Eror during connection: %@", [error description]);
}


- (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.
}

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


- (void)dealloc {
[myMent release];
[spotlight release];
[news release];
[popular release];
[dummyTable release];
[listOfEvents release];
   [super dealloc];
}


@end

 

Ευχαριστώ

Δημοσ.

Δεν ξέρω αν κατάλαβα καλά αυτό που θες καθώς δεν γνωρίζω τι είναι το αρχείο Json αλλά ξέρω ότι το γενικό σκεπτικό είναι το εξής:

 

- Τα δεδομένα πρέπει να περάσουν σε κάποια δομή NSArray ή NSDictionary

- Μετά η εφαρμογή υπακούει σε κάποια protocol

- Με βάση τα protocol τα δεδομένα μπορούν να "μπουν" σε table view

 

φιλικά,

Δημοσ.

Ευχαριστώ για την απάντηση. Ουσιαστικά έχω κολλήσει σε αυτό το σημείο - Τα δεδομένα πρέπει να περάσουν σε κάποια δομή NSArray ή NSDictionary. Δηλαδή ενώ καταιβάζω κανονικά τα δεδομένα πρέπει να φτιάξω ένα NSDictionary και δεν μπορώ να καταλάβω τη λογική... :( αν έχεις κάποιο καλό tutorial εκτός από το twiter θα ήμουν ευγνώμον!!!

Δημοσ.

ναι το είχα κοιτάξει αυτό...λογικά θα κάνει!!! απλά είμαι πολύ αρχάριος στη objective c και σκέφτοντας με λογική java πολλές φορές δημιουργεί προβληματάκια :P

Ευχαριστώ πολύ για τις απαντήσεις...

Πάντως για όποιον θέλει να ασχοληθεί καλό είναι να διαβάσει αυτό και να το έχει σαν όδηγό στην αρχή!!!

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...