inserting NSURL or integer in NSDictionary - iphone

i have a class 'book.h' in which i have declared
Book.h
NSInteger WeatherID;
NSString *Description;
NSURL *PictureURL;
BookDetailview.h
#import <UIKit/UIKit.h>
#class Book, XMLAppDelegate;
#interface BookDetailViewController : UIViewController
{
IBOutlet UITableView *tableView;
XMLAppDelegate *appDelegate;
Book *aBook; //creating object of 'Book-class'
NSMutableDictionary *EmpDictionary;
NSMutableArray *EmpArray;
}
#property (nonatomic, retain) Book *aBook;
#property (nonatomic, retain) NSMutableArray *EmpArray;
#end
BookDetailView.m
(UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"creation of cell");
static NSString *CellIdentifier = #"Cell";
ImageCell *cell = (ImageCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // creating cell from ImageCell.m
NSLog(#"calling image class");
if (cell == nil) {
cell = [[[ImageCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; // allocating with frame
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSLog(#"abook ...pictutre url......%#" , aBook.PictureURL);
EmpDictionary=[[NSMutableDictionary alloc]init];
[EmpDictionary setValue:aBook.Description forKey:#"Description"];
[EmpDictionary setValue:aBook.PictureURL forKey:#"PictureURL"];
[EmpDictionary setValue:aBook.WeatherID forKey:#"WeatherID"];
EmpArray=[[NSMutableArray alloc]init];
[EmpArray addObject:EmpDictionary];
NSDictionary *itemAtIndex = [self.EmpArray objectAtIndex:indexPath.row];
[cell setData:itemAtIndex]; //call to 'setData' meyhod to ImageCell.m
NSLog(#"set text of a cell");
return cell;
}
ImageCell.m
-(void)setData:(NSDictionary *)dic {
self.titleLabel.text = [dic objectForKey:#"Description"];
self.urlLabel.text = [dic objectForKey:#"PictureURL"];
self.itemID = (NSInteger)[dic objectForKey:#"WeatherID"];
NSLog(#"setting objects for keys");
// setting up the imageView now
self.imageView.image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: [dic objectForKey:#"PictureURL"]]]];
NSLog(#"setting of image");
}

[dict setValue:value forKey:key];
[dict setObject:object forKey:key];

NSDictionary *dict1 = [[NSDictionary alloc] initWithObjectsAndKeys:
#"val1", #"key1", #"val2", #"key2", #"val3", #"key3",#"val4", #"key4",#"val5", #"key5",nil];

Related

How to keep the checkmark in a UITableView after the view disappears

I have a uitableview that's displaying multiple selections with a custom checkmark. When selected the rows value is save using NSUserDefaults. The problem is that despite the values being saved the checkmarks disappear from the table cell rows. I can't figure out why.
thanks for any help, I'm really stuck on this.
Here's the .h code:
#interface CategoriesViewController : UITableViewController {
NSString *selectedCategoryTableString;
NSString *jsonStringCategory;
int prev;
}
// arForTable array will hold the JSON results from the api
#property (nonatomic, retain) NSArray *arForTable;
#property (nonatomic, retain) NSMutableArray *arForIPs;
#property (nonatomic, retain) NSMutableArray *categorySelected;
#property (nonatomic, retain) NSString *jsonStringCategory;
#property(nonatomic, retain) UIView *accessoryView;
#end
and the .m code:
#implementation CategoriesViewController
#synthesize jsonStringCategory;
#synthesize arForTable = _arForTable;
#synthesize arForIPs = _arForIPs;
- (void)viewDidLoad
{
[super viewDidLoad];
self.arForIPs=[NSMutableArray array];
self.categorySelected = [[NSMutableArray alloc] init];
[self reloadMain];
self.tableView.allowsMultipleSelection = YES;
}
-(void) reloadMain {
jsonString = #"http:///******";
// Download the JSON
NSString *jsonString = [NSString
stringWithContentsOfURL:[NSURL URLWithString:jsonString]
encoding:NSStringEncodingConversionAllowLossy|NSUTF8StringEncoding
error:nil];
NSMutableArray *itemsTMP = [[NSMutableArray alloc] init];
// Create parser
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *results = [parser objectWithString:jsonString error:nil];
itemsTMP = [results objectForKey:#"results"];
self.arForTable = [itemsTMP copy];
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (int)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.arForTable 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];
[cell.textLabel setFont:[UIFont fontWithName: #"Asap-Bold" size: 14.0f]];
[cell.detailTextLabel setFont:[UIFont fontWithName: #"Asap-Bold" size: 14.0f]];
cell.accessoryView.hidden = NO;
}
UIImageView *cellAccessoryImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"icon-tick.png"]] ;
UIImageView *cellAccessoryNoneImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#""]] ;
if([self.arForIPs containsObject:indexPath]){
cell.accessoryView = cellAccessoryImageView;
} else {
cell.accessoryView = cellAccessoryNoneImageView;
}
// Get item from tableData
NSDictionary *item = (NSDictionary *)[_arForTable objectAtIndex:indexPath.row];
// encoding fix
NSString *utf8StringTitle = [item objectForKey:#"name"];
NSString *correctStringTitle = [NSString stringWithCString:[utf8StringTitle cStringUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding];
cell.textLabel.text = [correctStringTitle capitalizedString];
NSNumber *num = [item objectForKey:#"id"];
cell.detailTextLabel.text = [num stringValue];
cell.detailTextLabel.hidden = YES;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if([self.arForIPs containsObject:indexPath]){
[self.arForIPs removeObject:indexPath];
[self.categorySelected removeObject:[[self.arForTable objectAtIndex:indexPath.row] objectForKey:#"id"]];
} else {
[self.arForIPs addObject:indexPath];
[self.categorySelected addObject:[[self.arForTable objectAtIndex:indexPath.row] objectForKey:#"id"]];
NSLog(#"%# categorySelected",self.categorySelected);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSLog(#"%# defaults categorySelected",[defaults arrayForKey:#"selectedCategoryTableString"]);
NSString *string = [self.categorySelected componentsJoinedByString:#","];
[defaults setObject:string forKey:#"selectedCategoryTableString"];
NSLog(#"%# STRING",string);
}
[tableView reloadData];
}
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:NO];
[self.navigationController setNavigationBarHidden:YES animated:NO];
self.navigationController.toolbarHidden = YES;
}
First of all your code has lots of memory leaks, please do use the static analyzer and/or instruments to fix them, few for them are pretty obvious like you initialized the SBJSON parser and did not release it, itemsTMP is another.
I have rewritten your code to be much more efficient and memory friendly:
#interface CategoriesViewController : UITableViewController
{
NSArray *_items;
NSMutableArray *_selectedItems;
UIImageView *cellAccessoryImageView;
}
#end
#implementation CategoriesViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_selectedItems = [NSMutableArray new];
cellAccessoryImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"icon-tick.png"]] ;
[self reloadMain];
self.tableView.allowsMultipleSelection = YES;
}
- (void)reloadMain
{
NSString *jsonString = #"http:///******";
// Download the JSON
jsonString = [NSString
stringWithContentsOfURL:[NSURL URLWithString:jsonString]
encoding:NSStringEncodingConversionAllowLossy|NSUTF8StringEncoding
error:nil];
// Create parser
SBJSON *parser = [SBJSON new];
NSDictionary *results = [parser objectWithString:jsonString error:nil];
if (_items) [_items release];
_items = [[results objectForKey:#"results"] copy];
[parser release];
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (int)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_items 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];
[cell.textLabel setFont:[UIFont fontWithName: #"Asap-Bold" size: 14.0f]];
[cell.detailTextLabel setFont:[UIFont fontWithName: #"Asap-Bold" size: 14.0f]];
cell.accessoryView.hidden = NO;
}
NSDictionary *item = [_items objectAtIndex:indexPath.row];
if ([_selectedItems containsObject:item])
{
// preloaded image will help you have smoother scrolling
cell.accessoryView = cellAccessoryImageView;
}
else
{
cell.accessoryView = nil;
cell.accessoryType = UITableViewCellAccessoryNone;
}
// Get item from tableData
cell.textLabel.text = [[NSString stringWithCString:[[item objectForKey:#"name"] cStringUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding] capitalizedString];
cell.detailTextLabel.text = [[item objectForKey:#"id"] stringValue];
cell.detailTextLabel.hidden = YES;
item = nil;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSDictionary *item = [_items objectAtIndex:indexPath.row];
if ([_selectedItems containsObject:item])
{
[_selectedItems removeObject:item];
}
else
{
[_selectedItems addObject:item];
}
item = nil;
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (void)dealloc
{
[_selectedItems release];
[cellAccessoryImageView release];
[super dealloc];
}
#end
Since in your table there is only one section. Try this approach and this will help you certainly.
In - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath write following code;
if([self.arForIPs containsObject:[NSNumber numberWithInt:indexPath.row]]){
cell.accessoryView = cellAccessoryImageView;
} else {
cell.accessoryView = cellAccessoryNoneImageView;
}
And in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath write code as below,
if([self.arForIPs containsObject:[NSNumber numberWithInt:indexPath.row]]){
[self.arForIPs removeObject:[NSNumber numberWithInt:indexPath.row]];
} else {
[self.arForIPs addObject:[NSNumber numberWithInt:indexPath.row]]
}

Second view in UITableView will not display parsed data from JSON

I have a parsed data from JSON file. It works like a charm in the first view of my UITableView. However, it displays a blank second view when I tap on an item.
MasterView.h
#interface MasterViewController : UITableViewController
{
NSArray *json;
}
#property (nonatomic, retain) NSArray *json;
#end
MasterView.m
#import "MasterViewController.h"
#import "InformationViewController.h"
#interface MasterViewController ()
#end
#implementation MasterViewController
#synthesize json;
- (void)viewDidLoad
{
[super viewDidLoad];
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://j4hm.t15.org/ios/console.php"]];
[self performSelectorOnMainThread: #selector(fetchedData:) withObject: jsonData waitUntilDone: YES];
}
- (void)fetchedData:(NSData *)responseData
{
NSError *error;
self.json = [NSJSONSerialization JSONObjectWithData: responseData options: kNilOptions error: &error];
NSLog(#"String is %#", self.json);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [json 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:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.text = [[json objectAtIndex: indexPath.row] objectForKey: #"Console"];
[cell setAccessoryType: UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
InformationViewController *informationViewController = [[InformationViewController alloc] initWithStyle:UITableViewStylePlain];
informationViewController.title = [[json objectAtIndex: indexPath.row] objectForKey: #"Console"];
informationViewController.Information = [[json objectAtIndex: indexPath.row] objectForKey: #"Model"];
NSLog(#"String is %#", informationViewController.Information);
[self.navigationController pushViewController: informationViewController animated:YES];
}
InformationView.h
#interface InformationViewController : UITableViewController
#property (nonatomic, strong) NSArray * Information;
#end
InformationView.m
#interface InformationViewController ()
#end
#implementation InformationViewController
#synthesize Information;
- (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];
}
// Configure the cell...
cell.textLabel.text = [[Information objectAtIndex: indexPath.row] objectForKey: #"Model"];
[cell setAccessoryType: UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
Here i Posted Another Answer Just Because i don't want to Generate Confusion.
The Project Code : http://www.cogzentappz.com/demo/iostutorial/TestJson.zip
instead of passing the Selected Value, Pass the Whole Array through NSUsedDefaults And Retrive them Using Following Code.
- (void)fetchedData:(NSData *)responseData
{
NSError *error;
self.json = [NSJSONSerialization JSONObjectWithData: responseData options: kNilOptions error: &error];
NSLog(#"String is %#", self.json);
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *arrayObj = [[NSMutableArray alloc] init];
for(int i = 0 ; i<[self.json count] ; i++) {
[arrayObj addObject:[json objectAtIndex:i]]];
}
[standardDefaults setObject:arrayObj forKey:#"longArray"];
[arrayObj release];
}
In the Next View you can Retrive the data Using the Below method.
in ViewDidLoad of informationViewcontroller use This
//reading
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
NSArray *arrayObj = [standardDefaults objectForKey:#"longArray"];
for(int i = 0 ; i<[arrayObj count] ; i++) {
NSLog(#"String is %#",[arrayObj objectAtIndex:i]);
}
As per Your Link The Json Output is like Below :
NSArray-->NSDictionary-->NSArray-->NSDictionary-->NSArray-->NSDictionary
Go to the link : http://json.bloople.net and Post all your json output and Hit enter.
So you have to get the values According to that format.
for(int i = 0 ; i<[self.json count] ; i++) {
NSArray *info_Array=[[self.json objectAtIndex:i ]valueForKey:#"Information"];
NSLog(#"info_Array is %#", info_Array);
for(int j = 0 ; j<[info_Array count] ; j++)
{
NSLog(#"Model is %#", [[info_Array objectAtIndex:j ]valueForKey:#"Model"]);
NSArray *title_Array=[[info_Array objectAtIndex:j ]valueForKey:#"Title"];
for(int k = 0 ; k<[title_Array count] ; k++)
{
NSLog(#"Game is %#", [[title_Array objectAtIndex:k ]valueForKey:#"Game"]);
NSLog(#"Publisher is %#", [[title_Array objectAtIndex:k ]valueForKey:#"Publisher"]);
}
}
NSString *Console_string= [[dataArray objectAtIndex:i ]valueForKey:#"Console"];
NSLog(#"String is %#", Console_string);
}
NSString *Console_string= [[self.Json objectAtIndex:i ]valueForKey:#"Console"];
NSLog(#"String is %#", Console_string);
}
I think you have to reloadData after you fetch your data. Right now, you specify that the NSArrray *json is empty, since the count returns 0. Thus, you prohibit the table view from being filled. Call [tableView reloadData]; after you fetch your data.
Change your code of MaterView.m with this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
InformationViewController *informationViewController = [[InformationViewController alloc] initWithStyle:UITableViewStylePlain];
informationViewController.title = [[json objectAtIndex: indexPath.row] objectForKey: #"Console"];
informationViewController.Information = [[json objectAtIndex: indexPath.row] objectForKey: #"Model"];
NSLog(#"String is %#", informationViewController.Information);
[self.navigationController pushViewController: informationViewController animated:YES];
}
Hope it helps you...

problem with NSMutableArray

i declared a NSMutable array and assigned some values to it .
.h
NSMutableArray *imageDetailsFromCategory;
#property (nonatomic, retain) NSMutableArray *imageDetailsFromCategory;
.m
#synthesise imageDetailsFromCategory
in ViewDidLoad:
imageDetailsFromCategory = [[NSMutableArray alloc]init];
//assigning object to Array..working fine.showing two images.
imageDetailsFromCategory = [self getImageDetailsFromCategory:generatedString];
i had one problem that solved now. i have a problem that is i am passing this array to StaticCategoryWithMultiImagePopOver class.
StaticCategoryWithMultiImagePopOver *staticCategoryWithMultiImagePopOver = [[StaticCategoryWithMultiImagePopOver alloc] init];
[staticCategoryWithMultiImagePopOver setParentImageDetailsArray:imageDetailsFromCategory];
in StaticCategoryWithMultiImagePopOver.h
NSMutableArray *nsmResult;
#property (nonatomic,retain)NSMutableArray *nsmResult;
.m
#synthesize nsmResult
-(void)setParentImageDetailsArray:(NSMutableArray *)imageDetailsFromCategoryFromParent{
nsmResult=[[NSMutableArray alloc] init];
nsmResult=[imageDetailsFromCategoryFromParent retain];
}
the passed array hold a class object with some string variables at each index.
so i am getting this through the code:
- (UITableViewCell *)tableView:(UITableView *)tableView15 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView15 dequeueReusableCellWithIdentifier:CellIdentifier];
// if (cell == nil) {
// cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
// }
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
NSString *documentsDirectory = [paths objectAtIndex:0];
SymbolTalkEntry *symbolTalkEntry = [[SymbolTalkEntry alloc]init];
symbolTalkEntry =[nsmResult objectAtIndex:indexPath.row];
NSString *imageNme = symbolTalkEntry.fileName; // *this line*
the above line is getting error.
array shows the count but the objects are out of scope..cant get the values ...
can any one tell me how can i access it...may i know what is the problem with me...
cellForRowAtIndexPath(its working fine)
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView15 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView15 dequeueReusableCellWithIdentifier:CellIdentifier];
// if (cell == nil) {
// cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
// }
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
NSString *documentsDirectory = [paths objectAtIndex:0];
SymbolTalkEntry *symbolTalkEntry = [[SymbolTalkEntry alloc]init];
symbolTalkEntry =[nsmResult objectAtIndex:indexPath.row];
NSString *imageNme = symbolTalkEntry.fileName;
[symbolTalkEntry release];
//Display image from app bundle
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.png",imageNme]];
cell.imageView.image= [UIImage imageWithContentsOfFile:path];
return cell;
}
-(NSInteger)number
This
nsmResult=[[NSMutableArray alloc] init];
nsmResult=[imageDetailsFromCategoryFromParent retain];
and this
SymbolTalkEntry *symbolTalkEntry = [[SymbolTalkEntry alloc]init];
symbolTalkEntry =[nsmResult objectAtIndex:indexPath.row];
are memory leaks.
About your problem: the object you get by
symbolTalkEntry =[nsmResult objectAtIndex:indexPath.row];
seems not to have a property fileName.
I think you should read a good book about Objective-C and Cocoa.
Actually it's the problem of the class which is inside the array.
I changed my class as this:
#import <Foundation/Foundation.h>
#interface SymbolTalkEntry : NSObject {
int categoryId;
NSString *runModeType;
NSString *sceneCategory;
NSString *fileName;
int isDefault;
int sortX;
int pathId;// 0 for appbundle 1 for document directry
}
#property (nonatomic, retain)NSString *runModeType;;
#property (nonatomic, retain)NSString *sceneCategory;
#property (nonatomic, retain)NSString *fileName;
-(id)init;
-(void) setCategoryId:(int) ctgId;
-(int)categoryId;
-(void) setRunModeType:(NSString *)rmType;
-(NSString *) runModeType;
-(void) setSceneCategory:(NSString *)scCategory;
-(NSString *) sceneCategory;
-(void) setFileName:(NSString *)Flname;
-(NSString *) fileName;
-(void) setIsDefault:(int) isDeft;
-(int)isDefault;
-(void) setSortX:(int) srtX;
-(int)sortX;
-(void) setPathId:(int) srtX;
-(int)pathId;
#end
[5:05:00 AM] Shamsudheen TK: #import "SymbolTalkEntry.h"
#implementation SymbolTalkEntry
#synthesize runModeType;
#synthesize sceneCategory;
#synthesize fileName;
-(id) init{
categoryId = 0;
runModeType = #"";
sceneCategory =#"";
fileName = #"";
isDefault = 0;
sortX =0;
pathId =0;
return self;
}
-(void) setCategoryId:(int) ctgId{
categoryId = ctgId;
}
-(int)categoryId{
return categoryId;
}
-(void) setRunModeType:(NSString *)rmType{
if (runModeType != rmType) {
[runModeType release ];
runModeType = [rmType retain];
}
}
-(NSString *) runModeType{
return runModeType;
}
-(void) setSceneCategory:(NSString *)scCategory{
if (sceneCategory != scCategory) {
[sceneCategory release];
sceneCategory = [scCategory retain];
}
}
-(NSString *) sceneCategory{
return sceneCategory;
}
-(void) setFileName:(NSString *)Flname{
if (fileName != Flname) {
[fileName release];
fileName = [Flname retain];
}
}
-(NSString *) fileName{
return fileName;
}
-(void) setIsDefault:(int) isDeft{
isDefault = isDeft;
}
-(int)isDefault{
return isDefault;
}
-(void) setSortX:(int) srtX{
sortX =srtX;
}
-(int)sortX{
return sortX;
}
-(void) setPathId:(int) srtX{
pathId = srtX;
}
-(int)pathId{
return pathId;
}
-(void)dealloc{
[categoryId release];
[runModeType release];
[sceneCategory release];
[fileName release];
[isDefault release];
[sortX release];
[pathId release];
}
#end
and set the values using the set methods that I wrote (eg: [classobj setCategoryId:1]).
now out of scope is solved.....

Can't get custom object from NSMutableArray (IPHONE SDK)

I have a problem with a NSMutableArray... After I fill the NSMutableArray in my function (readRoutes) I can't extract the objects.... Inside the readRoutes function I can... I can't figure where is the problem..
First the NSMutableArray is fill with Route objects in the readRoutes method...(Inside RoutesListView file) (I need to fill after TableView cells with these Route objects...)
This is the Route.h file :
#interface Route : NSObject {
NSString *routeId;
NSString *routeDirection;
NSString *routeName;
NSString *routeCode;
}
#property (nonatomic,retain) NSString *routeId;
#property (nonatomic,retain) NSString *routeDirection;
#property (nonatomic,retain) NSString *routeName;
#property (nonatomic,retain) NSString *routeCode;
-(id)initWithName:(NSString *)name direction:(NSString *)d routeId:(NSString *)rid code:(NSString *)c;
#end
The Route.m file:
#import "Route.h"
#implementation Route
#synthesize routeId;
#synthesize routeDirection;
#synthesize routeName;
#synthesize routeCode;
-(id)initWithName:(NSString *)name direction:(NSString *)d routeId:(NSString *)rid code:(NSString *)c{
self.routeId=rid;
self.routeName=name;
self.routeDirection=d;
self.routeCode=c;
return self;
}
#end
Now this is the RoutesListView.h file (a UITableViewController)
#import <UIKit/UIKit.h>
#import "Route.h"
#interface RoutesListView : UITableViewController <UITableViewDelegate,UITableViewDataSource> {
NSMutableArray *routeArray;
NSString *dbName;
NSString *dbPath;
}
#property (nonatomic,retain) NSMutableArray *routeArray;
#property (nonatomic,retain) NSString *dbName;
#property (nonatomic,retain) NSString *dbPath;
-(void)readRoutes;
#end
Inside the RoutesListView.m file :
#import "RoutesListView.h"
#import "Route.h"
#import <sqlite3.h>
#implementation RoutesListView
#synthesize routeArray;
#synthesize dbName,dbPath;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
dbName = #"data.db";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
dbPath = [documentsDir stringByAppendingPathComponent:dbName];
routeId =[[NSMutableArray alloc] init];
//routeArray
NSMutableArray * array = [[NSMutableArray alloc] init];
self.routeArray = array;
[array release];
[self readRoutes];
[super viewDidLoad];
}
INSIDE THE readRoutes method when I fill the NSMutableArray (The method work fine...):
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSString *aId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
NSString *aDirection =[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
NSString *aCode = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
Route *maRoute = [[Route alloc] initWithName:aName direction:aDirection routeId:aId code:aCode];
// I fill the NSMutableArray here...
[routeArray addObject:maRoute];
[maRoute release];
[aId release];
[aDirection release];
[aName release];
[aCode release];
}
FINALLY WHEN I SET THE CELL TEXTLABEL...IT CRASH! :
- (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];
}
// Configure the cell...
NSUInteger row = [indexPath row];
Route *myRoute = (Route *)[routeArray objectAtIndex:row];
cell.textLabel.text = myRoute.routeName;
[myRoute release];
return cell;
}
Help me to understand... I lost my hair on that...
Thanks
Maxime
You are over releasing several objects which will usually cause your application to crash. Also your init function is not properly implemented inside of your Route.m file. Make the following changes.
#import "Route.h"
#implementation Route
#synthesize routeId;
#synthesize routeDirection;
#synthesize routeName;
#synthesize routeCode;
-(id)initWithName:(NSString *)name direction:(NSString *)d routeId:(NSString *)rid code:(NSString *)c{
//Must call supers implementation of init
self = [super init];
if(self)
{
self.routeId=rid;
self.routeName=name;
self.routeDirection=d;
self.routeCode=c;
}
return self;
}
#end
Next you are over releasing objects inside of readRoutes
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSString *aId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
NSString *aDirection =[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
NSString *aCode = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
Route *maRoute = [[Route alloc] initWithName:aName direction:aDirection routeId:aId code:aCode];
// I fill the NSMutableArray here...
[routeArray addObject:maRoute];
[maRoute release]; //This is right you called alloc/init
[aId release]; //This is wrong never retained,copy or alloc/init
[aDirection release];//Dont release here either
[aName release]; //Dont release
[aCode release]; //Dont release
}
And finally you are over releasing the route inside of the following code
- (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];
}
// Configure the cell...
NSUInteger row = [indexPath row];
Route *myRoute = (Route *)[routeArray objectAtIndex:row];
cell.textLabel.text = myRoute.routeName;
[myRoute release]; //Do not release route here
return cell;
}

NSMutbleArray - add/remove objects with properties - leaks

i have a leak issue in my app.
I'm trying to add and remove objects to a NSMutableArray.
Here is the class Demande :
#interface Demande : NSObject {
//attibuts de la classe demande
NSString *demId;
NSString *demStatut;
NSString *demTitle;
NSString *demCreated;
NSString *demIdCopro;
NSString *demIdImmeuble;
NSString *demIdLot;
NSString *demDescriptif;
NSString *demAuteur;
NSString *demIdAuteur;
NSString *demLoginAuteur;
}
#property (retain,nonatomic) NSString *demId;
#property (retain,nonatomic) NSString *demIdCopro;
#property (retain,nonatomic) NSString *demIdImmeuble;
#property (retain,nonatomic) NSString *demIdLot;
#property (retain,nonatomic) NSString *demDescriptif;
#property (retain,nonatomic) NSString *demStatut;
#property (retain,nonatomic) NSString *demTitle;
#property (retain,nonatomic) NSString *demCreated;
#property (retain,nonatomic) NSString *demAuteur;
#property (retain,nonatomic) NSString *demIdAuteur;
#property (retain,nonatomic) NSString *demLoginAuteur;
#end
#import "Demande.h"
#implementation Demande
#synthesize demId,demIdCopro,demIdImmeuble,demIdLot,demDescriptif,demStatut,demTitle,demCreated,demAuteur,demIdAuteur,demLoginAuteur;
- (void)dealloc {
[demId release];
[demIdCopro release];
[demId release];
[demIdImmeuble release];
[demIdLot release];
[demDescriptif release];
[demStatut release];
[demTitle release];
[demCreated release];
[demAuteur release];
[demIdAuteur release];
[demLoginAuteur release];
[super dealloc];
}
#end
What i try to do in MyTableViewController :
#interface MyTableViewController : UITableViewController {
IBOutlet UITableView *tableV;
NSMutableArray *tab_Demandes;
}
#end
#implementation MyTableViewController
- (void)requestDReloadDone:(ASIHTTPRequest *)request
{
NSData *responseData = [request responseData];
NSString *jsonString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
//NSLog(#"JSON: %#",jsonString);
NSDictionary *results = [jsonString JSONValue];
NSArray *demandes = [results objectForKey:#"Demandes"];
[tab_Demandes removeAllObjects]; // -------- CRASH HERE !!!, except the first time through the method because the array is empty
for (NSDictionary *demandeD in demandes)
{
//parse du JSON
NSString *titre = [demandeD objectForKey:#"Title"];
NSString *desriptif = [demandeD objectForKey:#"Description"];
NSString *Id = [demandeD objectForKey:#"Id"];
NSString *created = [demandeD objectForKey:#"Created"];
NSString *statut = [demandeD objectForKey:#"Statut"];
NSString *copropriete = [demandeD objectForKey:#"Copropriete"];
NSString *immeuble = [demandeD objectForKey:#"Immeuble"];
NSString *lot = [demandeD objectForKey:#"Lot"];
NSString *auteur = [demandeD objectForKey:#"Author"];
NSString *auteurId = [demandeD objectForKey:#"IdAuthor"];
NSString *auteurLogin = [demandeD objectForKey:#"Login"];
//Creation de l'objet
Demande *dem =[[Demande alloc] init];
dem.demTitle=titre;
dem.demId=Id;
dem.demCreated=created;
NSString *descriptifDecode = [desriptif stringByReplacingPercentEscapesUsingEncoding:
NSASCIIStringEncoding];
dem.demDescriptif=descriptifDecode;
dem.demIdCopro=copropriete;
dem.demIdImmeuble=immeuble;
dem.demIdLot=lot;
dem.demStatut=statut;
dem.demAuteur=auteur;
dem.demIdAuteur=auteurId;
dem.demLoginAuteur=auteurLogin;
[tab_Demandes addObject:dem];
[dem release];
}
[tableV reloadData];
[jsonString release];
}
On the first launch of the app my array tab_Demandes is empty so everything is fine [removeAllObjects] works as there are no objects in it.
When i reload the tab_Demandes i need to clear it. That's when the app crashes
I realised that if i do :
- (void)dealloc {
/*
[demId release];
[demIdCopro release];
[demId release];
[demIdImmeuble release];
[demIdLot release];
[demDescriptif release];
[demStatut release];
[demTitle release];
[demCreated release];
[demAuteur release];
[demIdAuteur release];
[demLoginAuteur release];
*/
[super dealloc];
}
in the Demande class i then can do
[tab_Demandes removeAllObjects]
in TableViewController.
But then i have the properties as leaked for each object Demande. It's like when i do [dem release] the properties are also rleased and when i want to do [removeAllObjects] from my tab_Demandes there are no properties to release and i get a Bad_Access.
If anyone has an idea ...
EDIT: cellForROw method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
CustomCellViewController *cell = (CustomCellViewController *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
// NSLog(#"Cell created");
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCellViewController" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[CustomCellViewController class]])
{
cell = (CustomCellViewController *)currentObject;
break;
}
}
}
// Set up the cell...
cell.lbl_demId.text=[NSString stringWithFormat:#"%#", [[tab_Demandes objectAtIndex:indexPath.row ] demId]];
cell.lbl_demCopro .text=[NSString stringWithFormat:#"%#", [[tab_Demandes objectAtIndex:indexPath.row] demIdCopro ]];
cell.lbl_demImmeuble.text=[NSString stringWithFormat:#"%#", [[tab_Demandes objectAtIndex:indexPath.row] demIdImmeuble]];
cell.lbl_demLot.text=[NSString stringWithFormat:#"%#", [[tab_Demandes objectAtIndex:indexPath.row] demIdLot]];
cell.lbl_demDescriptif.text=[NSString stringWithFormat:#"%#", [[tab_Demandes objectAtIndex:indexPath.row] demTitle]];
if ([[[tab_Demandes objectAtIndex:indexPath.row] demStatut] isEqualToString: #"Validée"])
{
cell.lbl_ImageView.image=[UIImage imageNamed:#"VDEM2.png"];
}
if([[[tab_Demandes objectAtIndex:indexPath.row] demStatut] isEqualToString: #"Initiée"])
{
cell.lbl_ImageView.image=[UIImage imageNamed:#"IDEM.png"];
}
if([[[tab_Demandes objectAtIndex:indexPath.row] demStatut] isEqualToString: #"Terminée"])
{
cell.lbl_ImageView.image=[UIImage imageNamed:#"TDEM.png"];
}
if([[[tab_Demandes objectAtIndex:indexPath.row] demStatut] isEqualToString: #"En coursée"])
{
cell.lbl_ImageView.image=[UIImage imageNamed:#"EDEM.png"];
}
return cell;
}
Where it tab_Demandes being intialized?
It's not enough to only declare it in the .h file, you must also tell the compiler to alloc and init it.
A simple fix might be replacing the line [tab_Demandes removeAllObjects]; with
if(!tab_Demandes) { // tab_Demandes is nil and has not previously been created
tab_Demandes = [[NSMutableArray arrayWithCapacity:10] retain];
} else {
[tab_Demandes removeAllObjects];
}
Don't forget to [tab_Demandes release] in your dealloc method