Adding values to Cell from JSON file - code provided - iphone

I have a JSON file, and i need to extract values from it and display on my tableview. Everything works fine.
{
"1": {
"name": "Jemmy",
"birthday": "1994-11-23"
},
"2": {
"name": "Sarah",
"birthday": "1994-04-12"
},
"3": {
"name": "Deb",
"birthday": "1994-11-23"
},
"4": {
"name": "Sam",
"birthday": "1994-11-23"
}
}
When i display the values, it doesn't get displayed in the order of 1,2,3,4 as given in the records. It just gets displayed randomly. I have included my code below, I need it to be modified so i could display the content in the order given above. Can someone please help ?
- (void)requestSuccessfullyCompleted:(ASIHTTPRequest *)request{
NSString *responseString = [request responseString];
SBJsonParser *parser = [SBJsonParser new];
id content = [responseString JSONValue];
if(!content){
return;
}
NSDictionary *personDictionary = content;
NSMutableArray *personMutableArray = [[NSMutableArray alloc] init];
for (NSDictionary *childDictionary in personDictionary.allValues)
{
Deal *deal = [[Deal alloc] init];
deal.name=[childDictionary objectForKey:#"name"];
deal.dob=[childDictionary objectForKey:#"birthday"];
[personMutableArray addObject:deal];
}
self.personArray = [NSArray arrayWithArray:personMutableArray];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
Person *person = [self.personArray objectAtIndex:indexPath.row];
cell = [[Cell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
cell.namelabel.text=person.name;
cell.doblabel.text=person.dob;
}
return cell;
}

You're not reusing your cell correctly. If the cell is being reused, you fail to configure it.
Your cellForRowAtIndexPath: should be this way:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[Cell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
Person *person = [self.personArray objectAtIndex:indexPath.row];
cell.namelabel.text=person.name;
cell.doblabel.text=person.dob;
return cell;
}
Note that this is correct ARC code, but in pre-ARC, you need to add autorelease to the end of the [Cell alloc] line.

Try this and also correct your cellForRowAtIndexPath: for reused cells
- (void)requestSuccessfullyCompleted:(ASIHTTPRequest *)request{
NSString *responseString = [request responseString];
SBJsonParser *parser = [SBJsonParser new];
id content = [responseString JSONValue];
if(!content){
return;
}
NSDictionary *personDictionary = content;
NSMutableArray *personMutableArray = [[NSMutableArray alloc] init];
NSArray *array = [personDictionary allKeys];
NSArray * sortedArray = [array sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
for (NSString *str in sortedArray)
{
NSDictionary *childDictionary = [personDictionary objectForKey:str];
Deal *deal = [[Deal alloc] init];
deal.name=[childDictionary objectForKey:#"name"];
deal.dob=[childDictionary objectForKey:#"birthday"];
[personMutableArray addObject:deal];
}
self.personArray = [NSArray arrayWithArray:personMutableArray];
}

You store the data from the JSON in a dictionary which doesn't store ordered data. You would have to get that data in order in an array or sort the array afterwords.
You could sort the array in which you store the dictionary data, if you wish to sort it alphabetically by name you can do this:
self.personArray = [personMutableArray sortedArrayUsingSelector:#selector(compare:)];
- (NSComparisonResult)compare:(Deal *)dealObject {
return [self.name compare:dealObject.name];
}
If you want the data to be represented just as the JSON you should do something like this:
for (int i = 1; i < [personDictionary.allValues count]; i++)
{
NSDictionary *childDictionary = [[personDictionary objectForKey:[NSString stringWithFormat:#"%d", i]];
Deal *deal = [[Deal alloc] init];
deal.name=[childDictionary objectForKey:#"name"];
deal.dob=[childDictionary objectForKey:#"birthday"];
[personMutableArray addObject:deal];
}

Related

getting data in connectionDidFinishLoading,but ubable to use those data in cellForRowAtIndexPath method

I am developing an iphone application. In this i have to generate user profile form dynamically according to field information coming from the server.
So, if there are 5 fields in response i want ton create 5 labels from those data to display in cell of uitableview.
Not that i am getting the name of fields for user profile, not the values of profile.
I want to generate form dynamically from those data.
I'm able to get those data in NSMutableArray but in cellForRowAtIndexPath method its showing null.
How can i solve this?
My code snippet is as follow.
-(void) connectionDidFinishLoading:(NSURLConnection *)connection {
if (connection)
{
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
//You've got all the data now
//Do something with your response string
// NSLog(#"Response:%#",responseString);
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *object = [parser objectWithString:responseString error:nil];
NSString *pec_count = [object valueForKey:#"peculiarity_count"];
NSDictionary *pecs = [object valueForKey:#"peculiarities"];
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:nil];
[array addObject:#""];
for (int j= 1; j <= [pec_count integerValue] ; j++) {
NSString *val = [NSString stringWithFormat:#"%#%d",#"pec_",j];
NSString *pec_i = [pecs valueForKey:val];
NSString *modifiedString = [pec_i stringByReplacingOccurrencesOfString:#"_" withString:#" "];
NSString *capitalisedSentence = [modifiedString stringByReplacingCharactersInRange:NSMakeRange(0,1)
withString:[[modifiedString substringToIndex:1] capitalizedString]];
[array insertObject:capitalisedSentence atIndex:j];
}
self.peculiarity = array;
[self.table reloadData];
}
for (int j=0 ; j < [self.peculiarity count] ; j++) {
NSLog(#"info:%#", [self.peculiarity objectAtIndex: j]);
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIButton *racebtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
racebtn.frame = CGRectMake(240, 7, 10, 15);
[racebtn setBackgroundImage:[UIImage imageNamed:#"select.png"] forState:UIControlStateNormal];
[racebtn addTarget:self action:#selector(selectRace:)forControlEvents:UIControlEventTouchUpInside];
NSLog(#"cell=%#",[self.peculiarity objectAtIndex:3]);
}
Any help will be appreciated.
Thank you.
in connectionDidFinishLoading: method replace the following line:
self.peculiarity = array; with,
self.peculiarity = [[NSMutableArray alloc] initWithArray:array];
and in cellForRowAtIndexPath method add the following line of code:
cell.textLabel.text = [self.peculiarity objectAtIndex:indexPath.row];
hope this will help you.
Note: If you did not allocated self.peculiarity NSMutableArray then alloc that array like bellow..
if (self.peculiarity == nil)
self.peculiarity = [[NSMutableArray alloc] init];
and then assign that array to this array like bellow..
self.peculiarity = array;
after in cellForRowAtIndexPath: method just set that value or name to the textLabel Of the UITableViewCell like bellow..
cell.textLabel.text = [self.peculiarity objectAtIndex:indexPath.row];
see whole example with that method..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIButton *racebtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
racebtn.frame = CGRectMake(240, 7, 10, 15);
[racebtn setBackgroundImage:[UIImage imageNamed:#"select.png"] forState:UIControlStateNormal];
[racebtn addTarget:self action:#selector(selectRace:)forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:racebtn];
cell.textLabel.text = [self.peculiarity objectAtIndex:indexPath.row];
}
return cell;
}
I already have checked all above solutions before, Then finally i got solution for this. The problem was that my cell was getting loaded before arrival of data. So i used synchronous request to the server.
Code is below for this :
NSString *path = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"path"];
NSString *address = [NSString stringWithFormat:#"%#%#%#%#", path,#"users/",#"peculiarity/",self.tablename];
NSURL *URL = [NSURL URLWithString:address];
NSLog(#"%#",address);
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[URL host]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLCacheStorageAllowedInMemoryOnly
timeoutInterval:60.0];
[request setHTTPMethod:#"GET"];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (data) {
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"data:%#",responseString);
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *object = [parser objectWithString:responseString error:nil];
pecs = [object valueForKey:#"pec_values"];
for (int j =0; j < [pecs count] ; j++) {
NSLog(#"values:%#",[pecs objectAtIndex:j]);
}
self.peculiarity = array;
}
else {
// Handle error by looking at response and/or error values
NSLog(#"%#",error);
}

ios - How to load data from JSON url in UITableView?

I'm new to iPhone development,I'm trying to bind data from JSON Url in UITableview, but I'm getting an error in the below code.
- (void)viewDidLoad
{
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://80f237c226fa45aaa09a5f5c82339d46.cloudapp.net/DownloadService.svc/Courses"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
statuses = [parser objectWithString:json_string error:nil];
[self.dropdownTblView reloadData];
for (NSDictionary *status in statuses)
{
_altTitle = [status valueForKey:#"Title"];
NSLog(#"Title %#",_altTitle);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"%d",[statuses count]);
return [statuses count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
//Here I'm getting an error
id obj = [statuses objectAtIndex:indexPath.row];
NSString *name = [obj valueForKey:#"Title"];
cell.textLabel.text =name;
return cell;
}
This is my JSON
[
{
"Id": 1,
"Title": "Tamil to English",
"AltTitle": "த|மி|ழ்| |மூ|ல|ம்| |ஆ|ங்|கி|ல|ம்",
"Description": "Learn English through Tamil",
"Code": 1,
"Version": "1.0",
"SourceLanguageIndicator": "அ",
"TargetLanguageIndicator": "A",
"SourceLanguageCode": "ta",
"TargetLanguageCode": "en",
"Updated": "2013-02-21T03:33:19.6601651+00:00"
}
]
Any ideas? Thanks in advance.
You're returning the cell twice in the same scope, try to delete the first return cell;, also you're calling reloadData on the table just before the for loop; in this case probably the dataSource of the table is still empty, so call reloadData just after the for loop.
EDIT:
It's strange what is happening, the objectFromString must return a NSArray or a NSDictionary but it seems that is pointing to an NSSet. I can suggest 2 things in addition:
It seems that you're not usig ARC since you're calling autorelease on the UITableViewCell. In this case you're leaking parser and json_string in the viewDidLoad, so release them.
Make sure to call [super viewDidLoad]; (you're not doing this in your code).
Maybe because you set the type of obj as id. It is too generic and may not know how to respond to valueForKey. Have you tried declaring obj as an NSDictionary * instead? Like the code below:
NSDictionary *obj = [statuses objectAtIndex:indexPath.row];
NSString *name = [obj valueForKey:#"Title"];
Use the Below code in your ViewDidload and Run the Project , it will work.
Note :You're returning the cell twice, try to delete the first return
cell;
- (void)viewDidLoad
{
[super viewDidLoad];
statuses=[[NSMutableArray alloc]init];
NSURL *myURL = [NSURL URLWithString:#"http://80f237c226fa45aaa09a5f5c82339d46.cloudapp.net/DownloadService.svc/Courses"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSLog(#"Finished with status code: %i", [(NSHTTPURLResponse *)response statusCode]);
id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(#"jsonObject=%#",jsonObject);
statuses=[jsonObject mutableCopy];
[self.coursetable reloadData];
}];
NSLog(#"myURL=%#",myURL);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"%d",[statuses count]);
return [statuses count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
id obj = [statuses objectAtIndex:indexPath.row];
cell.textLabel.text =[obj valueForKey:#"Title"];
return cell;
}
Yes I was thinking right, statuses is not NSArray, it is NSSet so if you call objectAtIndex on NSSet it will crash.To get array convert NSSet into NSArray.
NSArray *array =[statuses allObjects];
- (void)viewDidLoad
{
NSString *dictStr=#"{\"Id\": 1,\"Title\": \"Tamil to English\",\"AltTitle\": \"த|மி|ழ்| |மூ|ல|ம்| |ஆ|ங்|கி|ல|ம்\",\"Description\": \"Learn English through Tamil\",\"Code\": 1,\"Version\": \"1.0\",\"SourceLanguageIndicator\": \"அ\",\"TargetLanguageIndicator\": \"A\",\"SourceLanguageCode\": \"ta\",\"TargetLanguageCode\": \"en\",\"Updated\": \"2013-02-21T03:33:19.6601651+00:00\"}";
NSDictionary *rootDict =[NSJSONSerialization JSONObjectWithData: [dictStr dataUsingEncoding:NSUTF8StringEncoding]
options: NSJSONReadingMutableContainers
error: nil];
NSArray *keys=[[rootDict allKeys] sortedArrayUsingSelector:#selector(compare:)];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [keys count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=#"cellIdentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text=[rootDict valueForKey:[keys objectAtIndex:indexPath.row]];
return cell;
}
remember rootDict and keys will be declared as global.

How to Parse certain arrays in SBJson

Hi I have this code here
- (void)viewDidLoad
{
[super viewDidLoad];
jsonURL = [NSURL URLWithString:#"http://oo.mu/json.php"];
jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL usedEncoding:nil error:nil];
self.jsonArray = [jsonData JSONValue];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *jsonObject = [parser objectWithString:jsonData error:NULL];
NSArray *list = [jsonObject objectForKey:#"Dewan's Party"];
for (NSDictionary *lesson in list)
{
// 3 ...that contains a string for the key "stunde"
NSString *content = [lesson objectForKey:#"Street"];
NSLog(#"%#", content);
}
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [jsonArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
return cell;
}
I'm using this JSon.php file.
{"Steffan's Party":{"Street":"774 Hoodwinked Avenue","City":"Sacramento","State":"California"},"Dewan's Party":{"Street":"2134 Statewide Lane","City":"New York","State":"New York"},"Austin's Party":{"Street":"9090 Gravink Court","City":"Woodland","State":"California"}}
This is a link to the website http://oo.mu/json.php.
What I'm trying to do here is parse each array in it's own UITableView cell. Example below, how can I do this?
Table Cell 1:
Steffan's Party
774 Hoodwinked Ave
Sacramento, California
Table Cell 2:
Dewan's Party
2134 Statewide Lane
New York, New York
How can I do this?
To get all keys and values from your current json do the following
in your viewDidLoad
NSString *yourJson = #"{\"Steffan's Party\":{\"Street\":\"774 Hoodwinked Avenue\",\"City\":\"Sacramento\",\"State\":\"California\"},\"Dewan's Party\":{\"Street\":\"2134 Statewide Lane\",\"City\":\"New York\",\"State\":\"New York\"},\"Austin's Party\":{\"Street\":\"9090 Gravink Court\",\"City\":\"Woodland\",\"State\":\"California\"}}";
SBJSON *json = [[SBJSON alloc] init];
NSDictionary *dic = [json objectWithString:yourJson error:NULL];
//NSMutableArray * keys; is defined in your interface .h file
//NSMutableArray * values; is defined in your interface .h file
keys = [[NSMutableArray alloc] init];
values = [[NSMutableArray alloc] init];
for(NSString *key in dic.keyEnumerator)
{
[keys addObject:key];
[values addObject:[dic objectForKey:key]];
}
now in your cellForRowAtIndexPath would look like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
//Steffan's Party 774 Hoodwinked Ave Sacramento, California
NSString *partyName = [keys objectAtIndex:indexPath.row];
NSDictionary *dicValues = [values objectAtIndex:indexPath.row];
NSString *Street = [dicValues objectForKey:"Street"];
NSString *City = [dicValues objectForKey:"City"];
NSString *State = [dicValues objectForKey:"State"];
//Steffan's Party 774 Hoodwinked Ave Sacramento, California
NSString *fullString = [NSString stringWithFormat:#"%# %# %# %#", partyName, Street, City, State];
return cell;
}
Why are you using SBJsonParser? It's easier using only -JSONValue! Look:
NSDictionary *jsonDict = [jsonData JSONValue];
// you can declare it as ivar
Then, in -tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
// that's how you can get key by index
NSString *key = [[jsonDict allKeys] objectAtIndex:indexPath.row];
NSDictionary *party = [jsonDict objectForKey:key];
NSString *street = [party valueForKey:#"Street"];
NSString *city = [party valueForKey:#"City"];
NSString *state = [party valueForKey:#"State"];
cell.textLabel.text = [NSString stringWithFormat:#"%# %# %#, %#", key, street, city, state];
return cell;
}

Turning additional strings in this JSON array into a UITableView Detail

I have a NSURL request that brings back an array of "name" and "phone", "etc"... The "name" Key shows up fine on the master table, but I'm having trouble figuring out how to get the rest of the array to show up on the detail table when I select the row. (I have the DetailerTableViewController working to accept the view). Any help would be appreciated.
Thanks,
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [rowsArray count];
NSLog(#"row count: %#",[rowsArray count]);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
NSDictionary *dict = [rowsArray objectAtIndex: indexPath.row];
cell.textLabel.text = [dict objectForKey:#"name"];
cell.detailTextLabel.text = [dict objectForKey:#"loginName"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.0/255.0 green:207.0/255.0 blue:255.0/255.0 alpha:1.0];
self.title = NSLocalizedString(#"Master", #"Master");
NSURL *url = [NSURL URLWithString:#"http://10.0.1.8/~imac/iphone/jsontest.php"];
NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url];
NSLog(jsonreturn); // Look at the console and you can see what the results are
NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
NSError *error = nil;
// In "real" code you should surround this with try and catch
NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
if (dict)
{
rowsArray = [dict objectForKey:#"member"];
[rowsArray retain];
}
NSLog(#"Array: %#",rowsArray);
NSLog(#"count is: %i", [self.rowsArray count]);
[jsonreturn release];
}
You need to implement:
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
to handle row selection and to push the view controller for the detail view.

Iphone xcode simulator crashes when I move up and down on a table row

I can't get my head round this. When the page loads, everything works fine - I can drill up and down, however 'stream' (in the position I have highlighted below) becomes not equal to anything when I pull up and down on the tableview. But the error is only sometimes. Normally it returns key/pairs.
If know one can understand above how to you test for // (int)[$VAR count]} key/value pairs
in a NSMutableDictionary object
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *FirstLevelCell = #"FirstLevelCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstLevelCell];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstLevelCell] autorelease];
}
NSInteger row = [indexPath row];
//NSDictionary *stream = (NSDictionary *) [dataList objectAtIndex:row];
NSString *level = self.atLevel;
if([level isEqualToString:#"level2"])
{
NSMutableDictionary *stream = [[NSMutableArray alloc] init];
stream = (NSMutableDictionary *) [dataList objectAtIndex:row];
// stream value is (int)[$VAR count]} key/value pairs
if ([stream valueForKey:#"title"] )
{
cell.textLabel.text = [stream valueForKey:#"title"];
cell.textLabel.numberOfLines = 2;
cell.textLabel.font =[UIFont systemFontOfSize:10];
NSString *detailText = [stream valueForKey:#"created"];
cell.detailTextLabel.numberOfLines = 2;
cell.detailTextLabel.font= [UIFont systemFontOfSize:9];
cell.detailTextLabel.text = detailText;
NSString *str = #"http://www.mywebsite.co.uk/images/stories/Cimex.jpg";
NSData *imageURL = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:str]];
UIImage *newsImage = [[UIImage alloc] initWithData:imageURL];
cell.imageView.image = newsImage;
[stream release];
}
}
else
{
cell.textLabel.text = [dataList objectAtIndex:row];
}
return cell;
}
Thanks for your time
You are both leaking and over-releasing the stream dictionary:
NSMutableDictionary *stream = [[NSMutableArray alloc] init]; // <-- Create a new dictionary
stream = (NSMutableDictionary *) [dataList objectAtIndex:row]; // <-- Overwrite the reference with another dictionary. Previous dictionary is lost...
...
[stream release]; // <-- You are releasing an object you don't have the ownership.
You should remove the dictionary creation as it is useless and the release as you don't own the object.
I didn't really understand the question, but...
You can test for number of values in a dictionary by:
if ([[myDictionary allKeys] count] == someNumber) {
// do something...
}