Sectioned tableview problem - iphone

I have a database app i am making and i can extract the results from a sqlite database and put them in the table, however i need to make them sort alphabetically in sections.
So now i have this code
AArray = [[NSMutableArray alloc] init];
BArray = [[NSMutableArray alloc] init];
CArray = [[NSMutableArray alloc] init];
DArray = [[NSMutableArray alloc] init];
EArray = [[NSMutableArray alloc] init];
FArray = [[NSMutableArray alloc] init];
GArray = [[NSMutableArray alloc] init];
HArray = [[NSMutableArray alloc] init];
IArray = [[NSMutableArray alloc] init];
JArray = [[NSMutableArray alloc] init];
KArray = [[NSMutableArray alloc] init];
LArray = [[NSMutableArray alloc] init];
MArray = [[NSMutableArray alloc] init];
NArray = [[NSMutableArray alloc] init];
OArray = [[NSMutableArray alloc] init];
PArray = [[NSMutableArray alloc] init];
QArray = [[NSMutableArray alloc] init];
RArray = [[NSMutableArray alloc] init];
SArray = [[NSMutableArray alloc] init];
TArray = [[NSMutableArray alloc] init];
UArray = [[NSMutableArray alloc] init];
VArray = [[NSMutableArray alloc] init];
WArray = [[NSMutableArray alloc] init];
XArray = [[NSMutableArray alloc] init];
YArray = [[NSMutableArray alloc] init];
ZArray = [[NSMutableArray alloc] init];
and i have code to move each item from the database into the relevant array, this all works fine.
I then have this code:
All = [NSMutableDictionary dictionaryWithObjectsAndKeys:AArray,#"A",BArray,#"B",CArray,#"C",DArray,#"D",EArray,#"E",FArray,#"F",GArray,#"G",HArray,#"H",IArray,#"I",JArray,#"J",KArray,#"K",LArray,#"L",MArray,#"M",NArray,#"N",OArray,#"O",PArray,#"P",QArray,#"Q",RArray,#"R",SArray,#"S",TArray,#"T",UArray,#"U",VArray,#"V",WArray,#"W",XArray,#"X",YArray,#"Y",ZArray,#"Z",nil];
AllArray = [NSArray alloc];
AllArray = [AllArray initWithArray:[[All allKeys]sortedArrayUsingSelector:#selector(compare:)]];
and this
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return [AllArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSArray *Array = [All objectForKey:[AllArray objectAtIndex:section]];
return [Array count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
int sectionindex = section;
return [AllArray objectAtIndex:sectionindex];
}
When i run it works fine, however if i scroll up and down a few times the app crashes without any error message.
It is something to do with the sections as it crashes when i add them in, but i just cannot see what im doing wrong
Everythings declared in the h file and i #property and #synthesize the following
#property (nonatomic,retain) NSMutableDictionary *All;
#property (nonatomic,retain) NSArray *AllArray;
#property (nonatomic, retain) UITableView *TableView;
If anyone could help me it would be really great!
Thanks!

Your dictionary "All" does not seem to be retained. You appear to assign the dictionary object directly to the instance variable, not to the property (in which case you would have used self.All).
If your app crashes without a message, then make sure to enable the Breakpoints button in Xcode's toolbar. This will run the app in the debugger, which will give you more helpful information about the crash. Setting NSZombieEnabled to YES also helps.

Take a look here:
Documentation
The simplest approach is, to provide a sort selector (see the link for details):
sortedArray = [anArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];

Related

create navigationbar dynamatically in ios

This is the static navigation bar how i set in my project , but want to do in dynamic way, here is the two type of code...
viewsArray = [[NSArray alloc] init];
AfterloginViewController *toolsnavigation = [[AfterloginViewController alloc] init];
toolsnavigation.tabBarItem.image = [UIImage imageNamed:#"cool.png"];
[toolsnavigation setTitle:#"Tools"];
UINavigationController *nav0 = [[UINavigationController alloc] initWithRootViewController:toolsnavigation];
MapViewController *myridenavigation = [[MapViewController alloc] init];
myridenavigation.tabBarItem.image = [UIImage imageNamed:#"cool.png"];
[myridenavigation setTitle:#"Login"];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:myridenavigation];
viewsArray = [NSArray arrayWithObjects:nav0,nav1,nav2,nav3,nav4,nav5,nav6,nav7,nav8, nil];
tabbarController = [[UITabBarController alloc] init];
[tabbarController setViewControllers:viewsArray];
self.window.rootViewController = tabbarController;
Now i am getting data from the URL and want to assign it as dynamically navigation item. But i m puzzeled now, any idea how to do it.
NSString *loginstring = [NSString stringWithFormat:#"%#nvgationarray.php",mydomainurl];
NSMutableData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: loginstring]];
NSDictionary *allData = [NSJSONSerialization JSONObjectWithData:dataURL options:0 error:nil];
int i = 0;
for(NSDictionary *stat in allData)
{
NSString *ssprst = [stat objectForKey:#"tab_type"];
NSString *ssprst1 = [stat objectForKey:#"tab_name"];
NSString *ssprst2 = [stat objectForKey:#"tab_id"];
NSString *ssprst3 = [stat objectForKey:#"icon"];
NSLog(#"all data ===== :::: %# %# %# %#",ssprst,ssprst1,ssprst2,ssprst3);
NSLog(#"++++++++++++++++++++++++++++++++++++++++++++++");
AbcViewController *myridenavigation = [[AbcViewController alloc] init];
myridenavigation.tabBarItem.image = [UIImage imageNamed:#"cool.png"];
[myridenavigation setTitle:#"Login"];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:myridenavigation];
i++;
}
if you want to retain memory UINavigationController . I suggest you hold it in NSMutableArray.
In header file
#property(nonatomic, strong) NSMutableArray* arrayNavigationCont;
In implementation file
#synthesize arrayNavigationCont = _arrayNavigationCont;
do not forget also to init array in somewhere relevant like viewDidLoad
self.arrayNavigationCont = [[NSMutableArray alloc] init];
In your method
...
for(NSDictionary *stat in allData)
{
...
AbcViewController *myridenavigation = [[AbcViewController alloc] init];
myridenavigation.tabBarItem.image = [UIImage imageNamed:#"cool.png"];
[myridenavigation setTitle:#"Login"];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:myridenavigation];
[self.arrayNavigationCont addObject:nav1]
i++;
}
Later you can access you navigationConts somewhere again
for(int i = 0; i < [self.arrayNavigationCont count]; i++)
{
UINavigationController *nav1 = [self.arrayNavigationCont objectAtIndex:i];
//do sth with nav1 to your like
}

How and where to allocate memory?

I narrowed down my problem to not allocating memory properly. I have a NSMutableArray called subjects as a member of a Category. When I populate the category with the array of subjects it doesn't store.
I am not sure how and where to allocate the memory for subjects, which is what I believe my problem is.
Here is the snippet:
Category *cat = [[Category alloc] init];
cat.category_title = [result_categories stringForColumn:#"CATEGORY"];
QuotesAppDelegate *appDelegate = (QuotesAppDelegate *)[[UIApplication sharedApplication] delegate];
for (Subject *sb in appDelegate.subjects){
if([cat.category_title isEqualToString:sb.category_title]){
[cat.subjects addObject:sb];
NSLog(#"Adding subject: %# cat.subjects.count=%i", sb.title, cat.subjects.count);
}
}
(The above log does print cat.subject names but it doesn't print the subjects.count)
Have you allocate memory to cat.subjects like
cat.subject = [[NSMutableArray alloc] init];
or
subject = [[NSMutableArray alloc] init];
in your Category class ? if not then do it. This might be the one of the problem.
Update
first of all change your code from Category *cat = [Category alloc]; to Category *cat = [[Category alloc] init];
and change your init as
- (id)init {
self = [super init];
if (self) {
subjects = [[NSMutableArray alloc] init];
}
return self;
}

Array count Should be Show in table view

i want show my NSMutableArray Count in a UILabel ,,,,,,,,,,
can any one help me
NSMutableArray * anArray = [[NSMutableArray alloc] init];
UILabel * aLabel = [[UILabel alloc] init];
aLabel.text = [NSString stringWithFormat:#"%d", [anArray count]];

how to ask an array if an object is contained?

how can i ask an array if it contains an item and if it does it to [[NSArray alloc] initWithObjects:#"those objects" automatically.
this is my fav .h
#interface FavoriteViewController : UITableViewController {
NSMutableArray *favoritesArray;
NSMutableArray *didContain;
}
#property (nonatomic, retain) NSMutableArray *favoritesArray;
#property (nonatomic, retain) NSMutableArray *didContain;
this is the .m
favoritesArray = [[NSMutableArray alloc]init];
didContain = [[NSMutableArray alloc]init];
if
([favoritesArray containsObject:#"one"])
{
[didContain addObject:#"one"];
}
and in the detail view controller.m i have this code
[[NSMutableArray alloc] init];
[favoritesArray addObject: #"one"];
i get the table to work however nothing shows up....
NSArray *yourArray = [[NSArray alloc] initWithObjects:#"Hello", #"World", nil];
NSMutableArray *didContain = [[NSArray alloc] init];
if([yourArray contains: #"Hello"])
{
[didContain addObject:#"Hello"];
}
or
NSArray *yourArray = [[NSArray alloc] initWithObjects:#"Hello", #"World", nil];
NSMutableArray *didContain = [[NSArray alloc] init];
[didContain addObject: [yourArray objectAtIndex:[yourArray indexOfObject:#"Hello"]];
All of this and more is readily available in the apple docs. Please do some google searching first next time. Good luck hope this helps.
Use filteredArrayUsingPredicate:
See NSArray Class Reference and Predicate Programming Guide
It appears that you are trying to use an uninitialized property in your detail view controller.
Normally, you initialize properties in your init: or viewDidLoad method implementations then before presenting the view in your parent view controller, set the property using the property accessors
This line:
// DetailViewController.m initializer code
[[NSMutableArray alloc] init]; // returned object is not used
Should be:
favoritesArray = [[NSMutableArray alloc] init]; // view controller initialization code
Instead of calling this:
[favoritesArray addObject:#"one"];
After you create your detailViewController set the favoritesArray with the filtered array
// FavoriteViewController.m
MyDetailViewController *dvc = [[MyDetailViewController alloc] initWithNibName:#"MyDetailViewController" bundle:nil];
// populate the array
[dvc setFavoritesArray:didContain];
// Assuming you are using a navigation controller
[navigationController pushViewController:dvc animated:YES];
[dvc release];

Initializing a class. Can you see any problems with this?

-(id)init {
if (self = [super init]) {
self.name = [[NSString alloc] init];
self.type = [[NSString alloc] init];
self.phoneNumber = [[NSString alloc]init];
self.webAddress = [[NSString alloc] init];
NSMutableArray *pricesArray = [[NSMutableArray alloc] init];
NSMutableArray *poolsArray = [[NSMutableArray alloc] init];
self.prices = pricesArray;
self.pools = poolsArray;
[pricesArray release];
[poolsArray release];
//Create the address dictionaries
NSMutableDictionary *addressItems = [[NSMutableDictionary alloc] initWithObjectsAndKeys:#"", KAddressStreet1Key, #"", KAddressStreet2Key, #"", KAddressBoroughKey, #"", KAddressCityKey, #"", KAddressCountyKey, #"", KAddressPostCodeKey, #"" ,KAddressCountryKey, nil];
//Add dictionary to the array to contain address values
self.address = addressItems;
[addressItems release];
}
return self;
}
I'm currently doing a massive round of debugging thanks to EXC_BAD_ACCESS errors.. grr.
Does the code above seem fine and logical for a class init method? Basically I'm getting the EXC_BAD_ACCESS errors when I release both the pools (mutable array and the dictionary).
How are your properties declared? If they are not declared with retain then most of your objects will be deallocated at the end of this method.
You're leaking objects with each allocation for the string properties. Other than that, I don't notice anything wrong. How are the AddressXKeys defined?