iOS JSON Parsing to tableview, incorrect rows - mysql

I am developing an iOS App, I have a tableview controller populated from a MySQL database. When I click on a raw a detailview appears showing data from the database, such as a master detail app. The problem is following:
1. When I run the app on the iPhone simulator, the app shows the three records from the mysql table, thats correct.
2. If I click on the first row, nothing happened.
3. If I click on the second or third row, the app opens the detailview, but the data don't correspond to the selected row(record).
4. I click on the back button of the detailview, and the app opens the masterview again.
5. Now if I click on the first row, the app opens the detailview, but as mentioned before, the detailview data don't correspond to the selected row(record) on the master view.
Here you have my viewcontroller code (master view):
//
// ViewController.m
//
#import "ViewController.h"
#import "DetailViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Areas";
[UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
NSURL *url = [NSURL URLWithString:#""];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc]initWithRequest:request delegate:self];
// Do any additional setup after loading the view, typically from a nib.
}
- (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
{
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
news = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
[mainTableView reloadData];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *errorView = [[UIAlertView alloc]initWithTitle:#"Error" message:#"The download could not complete - please make sure you're connect to either 3G or WiFi" delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[errorView show];
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
}
-(int)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(int)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return [news count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
if(cell==nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MainCell"];
}
cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:#"name"];
cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:#"nombre"];
return cell;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
detailViewController.title =[[news objectAtIndex:indexPath.row]objectForKey:#"nombre"];
detailViewController.newsArticle = [news objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
And here the detailview code:
//
// DetailViewController.m
//
#import "DetailViewController.h"
#interface DetailViewController ()
#end
#implementation DetailViewController
#synthesize newsArticle;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
titleLabel.text = [newsArticle objectForKey:#"nombre"];
timeLabel.text = [newsArticle objectForKey:#"name"];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Any idea what I have done wrong??
Thank you

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
detailViewController.title =[[news objectAtIndex:indexPath.row]objectForKey:#"nombre"];
detailViewController.newsArticle = [news objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
}
You are using didDeselectRow which is called when a row is deselected and hence the wierd behaviour plus wrong data.. you have to use didSelectRow

Related

how to access object property in this case

project type: masterview application components first view: Tableview second view :webview what i want is to navigate in such a technique that : when user taps a cell, it should get the href property and to open in next webview, the page it links to.. instead of only href property
suggestion thanks
#import "MasterViewController.h"
#import "TFHpple.h"
#import "TFHppleElement.h"
#import "Preface.h"
#import "Chapters.h"
#import "Index.h"
#import "DetailViewController.h"
#interface MasterViewController ()
{
NSMutableArray * preface;
NSMutableArray * chapters;
NSMutableArray * index;
}
#end
#implementation MasterViewController
- (void) loadChapters
{
///// capturing the link /////
/*
NSURL pointer to store the file url
*/
NSURL * chapter_url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"test/mainpage" ofType:#"html"] isDirectory:NO];
NSData * chapter_data = [NSData dataWithContentsOfURL:chapter_url];
///// passing to TFHpple /////
TFHpple * chapter_parser = [TFHpple hppleWithHTMLData:chapter_data];
///// String for XPath Query /////
NSString * chapter_query_string = #"//div[#class='cD']/ul[2]/li/a";
NSArray * chapter_nodes = [chapter_parser searchWithXPathQuery:chapter_query_string];
///// Array for initializing elements of preface/////
NSMutableArray * chapter_contents = [[NSMutableArray alloc]initWithCapacity:0];
///// Now looping to fetch contents /////
for (TFHppleElement * element in chapter_nodes) {
Chapters * chapter_ptr =[[Chapters alloc]init];
[chapter_contents addObject:chapter_ptr];
chapter_ptr.chapter_title = [[element firstChild]content];
chapter_ptr.chapter_url = [element objectForKey:#"href"];
}
chapters= chapter_contents;
[self.tableView reloadData];
}
viewdidload
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadPreface];
[self loadChapters];
[self loadIndex];
// Do any additional setup after loading the view, typically from a nib.
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
switch (section) {
case 0:
return #"Table of Contents";
break;
case 1:
return #"Chapters";
break;
case 3:
return #"Index";
break;
}
return nil;
}
Method returning no of rows in a section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case 0:
return preface.count;
break;
case 1:
return chapters.count;
break;
case 2:
return index.count;
break;
}
return 0;
}
- (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];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
switch (indexPath.section) {
case 0:
{
Preface *preface_access = [preface objectAtIndex:indexPath.row];
cell.textLabel.text = preface_access.preface_title;
return cell;
}
case 1:
{
Chapters * chapter_access = [chapters objectAtIndex:indexPath.row];
cell.textLabel.text = chapter_access.chapter_title;
return cell;
}
case 2:
{
Index * index_access = [index objectAtIndex:indexPath.row];
cell.textLabel.text = index_access.index_title;
return cell;
}
default:
break;
}
return cell;
i want to do something like this
in detailviewcontroller.m
it is the 2nd view controller
this is pseudo code I am weak in OOP concepts so appologies
- (void)viewDidLoad
{
NSMutableArray * contentsfromprevious = [[NSMutableArray alloc]initWithCapacity:0];
Preface * data = [[Preface alloc]init];
data.preface_url = contentsfromprevious;
data = preface_contents;
[self.loadwebview.request];
}
First of all your question is still not clear enough. What I've come to understand that, you want something like this:
In the MasterViewController you wish show a list of your links (html pages stored locally).
If any of the table view cells are tapped, you want to display the locally stored html page in a web view on the DetailViewController.
Assuming this, you should implement this UITableViewDelegate method on your MasterViewController:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailVC = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
detailVC.htmlFileName = [htmlFiles objectAtIndex:indexPath.row]; // you might need to change this according to your need, as I said your requirement is unclear
[self.navigationController pushViewController: detailVC animated:YES];
}
Also you'll need to add this into your DetailViewController.h file
#property (nonatomic, strong) NSString *htmlFileName;
And after you've done this, you should change your DetailViewControllers viewDidLoad method into something like this:
- (void)viewDidLoad
{
// your code
// ....
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: htmlFileName ofType:#"html"] isDirectory:NO]]];
webView.scalesPageToFit = YES;
// your code
// ...
}
I am not sure if this is what you really want though. On the other note, you don't have to put break statements after any return statements. It never gets executed.

View controller not in view hierarchy?

So I am trying to transition from one view controller to another with a segue programmatically so that I can only initialize it when a user needs to login. However, xcode keeps giving me the error that it is not in the view hierarchy and refuses to load to it.
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#end
ViewController.m
#import "ViewController.h"
#import "API.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
if (![[API sharedInstance] isAuthorized]) {
[self performSegueWithIdentifier:#"ShowLogin" sender:nil];
}// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
LoginScreen.h (the controller not in the hierarchy)
#import <UIKit/UIKit.h>
#interface LoginScreen : UIViewController
{
//the login form fields
IBOutlet UITextField *fldUsername;
IBOutlet UITextField *fldPassword;
}
//action for when either button is pressed
-(IBAction)btnLoginRegisterTapped:(id)sender;
#end
LoginScreen.m (the controller apparently not in the hierarchy)
#import "LoginScreen.h"
#import "UIAlertView+error.h"
#import "API.h"
#include <CommonCrypto/CommonDigest.h>
#define kSalt #"(protected value)"
#implementation LoginScreen
-(void)viewDidLoad {
[super viewDidLoad];
}
#pragma mark - View lifecycle
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(IBAction)btnLoginRegisterTapped:(UIButton*)sender {
//form fields validation
if (fldUsername.text.length < 4 || fldPassword.text.length < 4) {
[UIAlertView error:#"Enter username and password over 4 chars each."];
return;
}
//salt the password
NSString* saltedPassword = [NSString stringWithFormat:#"%#%#", fldPassword.text, kSalt];
//prepare the hashed storage
NSString* hashedPassword = nil;
unsigned char hashedPasswordData[CC_SHA1_DIGEST_LENGTH];
//hash the pass
NSData *data = [saltedPassword dataUsingEncoding: NSUTF8StringEncoding];
if (CC_SHA1([data bytes], [data length], hashedPasswordData)) {
hashedPassword = [[NSString alloc] initWithBytes:hashedPasswordData length:sizeof(hashedPasswordData) encoding:NSASCIIStringEncoding];
} else {
[UIAlertView error:#"Password can't be sent"];
return;
}
//check whether it's a login or register
NSString* command = (sender.tag==1)?#"register":#"login";
NSMutableDictionary* params =[NSMutableDictionary dictionaryWithObjectsAndKeys:command, #"command", fldUsername.text, #"username", hashedPassword, #"password", nil];
//make the call to the web API
[[API sharedInstance] commandWithParams:params onCompletion:^(NSDictionary *json) {
//result returned
NSDictionary* res = [[json objectForKey:#"result"] objectAtIndex:0];
if ([json objectForKey:#"error"]==nil && [[res objectForKey:#"IdUser"] intValue]>0) {
[[API sharedInstance] setUser: res];
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
//show message to the user
[[[UIAlertView alloc] initWithTitle:#"Logged in" message:[NSString stringWithFormat:#"Welcome %#",[res objectForKey:#"username"]] delegate:nil cancelButtonTitle:#"Close" otherButtonTitles: nil] show];
} else {
//error
[UIAlertView error:[json objectForKey:#"error"]];
}
}];
}
#end
API.h (calling my web service)
#import "AFHTTPClient.h"
#import "AFNetworking.h"
typedef void (^JSONResponseBlock)(NSDictionary* json);
#interface API : AFHTTPClient
#property (strong, nonatomic) NSDictionary* user;
+(API*)sharedInstance;
//check whether there's an authorized user
-(BOOL)isAuthorized;
//send an API command to the server
-(void)commandWithParams:(NSMutableDictionary*)params onCompletion:(JSONResponseBlock)completionBlock;
-(NSURL*)urlForImageWithId:(NSNumber*)IdPhoto isThumb:(BOOL)isThumb;
#end
API.m (calling my web service)
#import "API.h"
//the web location of the service
#define kAPIHost #"(protected web address)"
#define kAPIPath #""
#implementation API
#synthesize user;
#pragma mark - Singleton methods
/**
* Singleton methods
*/
+(API*)sharedInstance {
static API *sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
sharedInstance = [[self alloc] initWithBaseURL:[NSURL URLWithString:kAPIHost]];
});
return sharedInstance;
}
#pragma mark - init
//intialize the API class with the deistination host name
-(API*)init {
//call super init
self = [super init];
if (self != nil) {
//initialize the object
user = nil;
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
// Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
[self setDefaultHeader:#"Accept" value:#"application/json"];
}
return self;
}
-(BOOL)isAuthorized {
return [[user objectForKey:#"IdUser"] intValue]>0;
}
-(void)commandWithParams:(NSMutableDictionary*)params onCompletion:(JSONResponseBlock)completionBlock {
NSData* uploadFile = nil;
if ([params objectForKey:#"file"]) {
uploadFile = (NSData*)[params objectForKey:#"file"];
[params removeObjectForKey:#"file"];
}
NSMutableURLRequest *apiRequest = [self multipartFormRequestWithMethod:#"POST" path:kAPIPath parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
if (uploadFile) {
[formData appendPartWithFileData:uploadFile name:#"file" fileName:#"photo.jpg" mimeType:#"image/jpeg"];
}
}];
AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest: apiRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//success!
completionBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//failure :(
completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:#"error"]);
}];
[operation start];
}
-(NSURL*)urlForImageWithId:(NSNumber*)IdPhoto isThumb:(BOOL)isThumb {
NSString* urlString = [NSString stringWithFormat:#"%#/%#upload/%#%#.jpg", kAPIHost, kAPIPath, IdPhoto, (isThumb)?#"-thumb":#""];
return [NSURL URLWithString:urlString];
}
#end
The issue is that the view of your initial view controller has not been added to the window by the time viewDidLoad is called.
Move that call to viewDidAppear:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (![[API sharedInstance] isAuthorized]) {
[self performSegueWithIdentifier:#"ShowLogin" sender:self];
}
}

json ios5 nothing at the screen

i have just install the xcode 4.2.1 with ios5 and i try to use json.
this my code
-(void)start
{
responseData = [[NSMutableData data]retain];
membreArray = [NSMutableArray array];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http:******.php"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[responseData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"error %#",error);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
jsonArray = [responseString JSONValue];
NSLog(#"array %#",jsonArray);
}
in my NSLog(#"array %#",jsonArray); i can see all records,everything is ok. But in my tableview nothing at the screen.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [jsonArray count];
}
// Customize the appearance of table view cells.
- (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];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// Configure the cell.
NSDictionary *dico = [self.jsonArray objectAtIndex:indexPath.row];
cell.textLabel.text = [dico objectForKey:#"name"];
return cell;
}
this code work on xcode 3 but not in xcode 4.
somes helps please.
After you finish loading the data, you need to tell your tableview to reload. I'm not seeing that in your code. Try something like this:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
jsonArray = [responseString JSONValue];
NSLog(#"array %#",jsonArray);
/* This tells your tableView to reload */
[tableView reloadData];
}
Also, make sure your TableView is connected in the designer via an IBOutlet. Otherwise your reloadData command will fall on deaf ears.

When selecting a row in table, it switches to blank screen

in part of my app (storyboard) I have a table view with various rows, and when I select a row, an html file should be displayed. Instead when I select a row, I get a blank screen instead.
I thought I needed another view controller after the tableview controller, but I get the same issue whether I have one or not.
In my table controller .m file I have the following code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
articleIndex = indexPath.row;
WebView *selectedArticle = [[WebView alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:selectedArticle animated:YES];
[selectedArticle release];
That is allowing me to select a row and the screen changes to a blank one, rather than loading the relevant html file.
I have in my resources folder a .h and .m file as follows:
webView.h
#import <UIKit/UIKit.h>
#import "globals.h"
#interface WebView : UIViewController{
NSArray *articleNames;
IBOutlet UIWebView *webView;
}
#property(nonatomic, retain)IBOutlet UIWebView *webView;
#end
webView.m
#import "WebView.h"
#implementation WebView
#synthesize webView;
- (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
- (void)viewDidLoad
{
[super viewDidLoad];
articleNames = [[NSArray arrayWithObjects:
#"chapter1",
#"chapter2",
#"chapter3",
#"chapter4",
#"chapter5",
#"chapter6",
nil] retain];
NSString *chapterName = [[NSString alloc] initWithString:[articleNames objectAtIndex:articleIndex]];
NSString *path = [[NSBundle mainBundle] pathForResource:chapterName ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
[chapterName release];
}
- (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 YES;
}
#end
I can't see what I have done wrong here, any advice? I'm not sure how to get the html files to display or where I need to make changes to allow it. If I remove the HTML files from the project the app crashes so it's definitely loading them but for some reason not displaying...
Well first off where is "articleIndex" I see you set it in your tableViewController but it is not webView.h or webView.m. I would suggest first adding in a NSLog
NSURL *url = [NSURL fileURLWithPath:path];
NSLog(#"Article URL:%#", url);
This will let you see if the url is correct first. Second it looks like you connected your UIWebView with interface builder, so make sure that the webView is connect correctly. You can also test the webView buy using:
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: #"http://www.apple.com"]];
And see if apple.com loads. If it does then you know your issue is with your html files or the paths to them.

NSDictionary selected on Actionsheet, from a UITableView with Custom Cells

I am working on a big learning experience, adding a NSDictionary that uses a JSON(deserializer) to grab content from my MAMP(myphp admin) server (localhost), and put all's this information nicely in a TablewView, which has custom UITableViewCell's,
my problem is, the Table View loads the information from my server just fine, however, when I use the didSelectRowatIndexPath, with an actionsheet, I can get the actionsheet to come up, however I cannot get it to pull the information from my NSDictionary that was previously used in the UITableView.... (basically in the end, the action sheet will have a few buttons, for example loading a URL from a shared application, and all this information will be grabbed from my server using the same NSDictionary in the UITable View.
any help would greatly be appreciated...... thank you so much this community rocks!!!
//
// TouchJSONViewController.m
// TouchJSON
//
// Created by Chance Brown on 3/7/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "TouchJSONViewController.h"
#import "CJSONDeserializer.h"
#import "StaffPicksCustomCell.h"
#import "UIImageView+WebCache.h"
#implementation TouchJSONViewController
#synthesize tableview, rows, cellOne;
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"http://localhost/json.php"]; // Modify this to match your url.
// HOME 192.168.2.23
NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url]; // Pulls the URL
// NSLog(jsonreturn); // Look at the console and you can see what the restults 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)
{
rows = [[dict objectForKey:#"users"] retain];
}
NSLog(#"Array: %#",rows);
[jsonreturn release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [rows count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
StaffPicksCustomCell *cell =(StaffPicksCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[StaffPicksCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
NSSortDescriptor *ratingsSortDescriptor = [[[NSSortDescriptor alloc] initWithKey:#"ID" ascending:NO] autorelease];
rows = [rows sortedArrayUsingDescriptors:[NSArray arrayWithObject:ratingsSortDescriptor]];
NSDictionary *dict = [rows objectAtIndex: indexPath.row];
cell.primaryLabel.text = [dict objectForKey:#"post_title"];
cell.theDate.text = [dict objectForKey:#"post_date"];
cell.mydescription.text = [dict objectForKey:#"post_content"];
[cell.myImageView setImageWithURL:[NSURL URLWithString:[dict objectForKey:#"imagelink"]]
placeholderImage:[UIImage imageNamed:#"placeholder1.png"]];
//cell.textLabel.text = [dict objectForKey:#"post_title"];
//cell.detailTextLabel.text = [dict objectForKey:#"post_content"];
//tableView.backgroundColor = [UIColor cyanColor];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 125;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
StaffPicksCustomCell *cell = (StaffPicksCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"StaffPicksCustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[StaffPicksCustomCell class]]) {
cell= (StaffPicksCustomCell *) currentObject;
break;
}
}
}
// Configure - Did Select Row at Index Path.
UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:#"OPTIONS" delegate:self cancelButtonTitle:#"Cancel"destructiveButtonTitle:nil otherButtonTitles:#"More Info",#"Contact Seller",#"Picture", nil];
[popup setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[popup showInView:[self view]];
[popup release];
// return cell
}
-(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)indexPath
{
switch (indexPath) {
case 0:
{
NSLog(#"Case 0 Selected");
NSDictionary *dict = [rows objectAtIndex: indexPath];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[dict objectForKey:#"guid"]]];
}
break;
case 1:
{
NSLog(#"Case 1 Selected");
}
break;
case 2:
{
NSLog(#"Case 2 Selected");
}
break;
}
}
- (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 {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
It's the rows, they are being autoreleased. Here's the important lines.
The first line in the viewDidLoad retains the rows correctly
rows = [[dict objectForKey:#"users"] retain];
But you sort these rows in the cellForRowAtIndexPath and did not retain it.
rows = [rows sortedArrayUsingDescriptors:[NSArray arrayWithObject:ratingsSortDescriptor]];
This should be something like:
NSArray *sortedRows = [rows sortedArrayUsingDescriptors:[NSArray arrayWithObject:ratingsSortDescriptor]];
[rows release];
rows = [sortedRows retain];
Hope this helps.