Array count Should be Show in table view - iphone

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]];

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
}

Load more pages in TTLauncherView

In my implementation of TTLauncherView, only loads the first page. Why?
I have 47 items in array, 47 items div 9 items by page, I should have 6 pages.
Thanks for helping.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSMutableString *jsonString = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSDictionary *results = [jsonString JSONValue];
NSArray *photos = [[results objectForKey:#"photosets"] objectForKey:#"photoset"];
launcherView = [[TTLauncherView alloc] initWithFrame:self.view.bounds];
launcherView.backgroundColor = [UIColor whiteColor];
launcherView.delegate = self;
launcherView.columnCount = 3;
launcherView.persistenceMode = TTLauncherPersistenceModeNone;
NSMutableArray *itemArray = [[NSMutableArray alloc] init];
for (NSDictionary *photo in photos)
{
NSString *iconURLString = [NSString stringWithFormat:#"http://farm%#.static.flickr.com/%#/%#_%#_s.jpg",
[photo objectForKey:#"farm"], [photo objectForKey:#"server"], [photo objectForKey:#"primary"], [photo objectForKey:#"secret"]];
NSDictionary *title = [photo objectForKey:#"title"];
NSString *itemTitle = [title objectForKey:#"_content"];
TTLauncherItem *itemMenu = [[[TTLauncherItem alloc] initWithTitle:itemTitle
image:iconURLString
URL:nil
canDelete:NO] autorelease];
[itemArray addObject:itemMenu];
}
launcherView.pages = [NSArray arrayWithObject: itemArray];
[self.view addSubview:launcherView];
}
As I recall, TTLauncherView doesn't break up the TTLauncherItem's into pages automatically. You need an array of arrays. All of the launcher item's in the first array will be on the first page, all the launcher item's in the second array will be on the second page etc. It has been a long time since I've used it, but I think that's how it worked.
My modified code with the hint of #Darren
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSMutableString *jsonString = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSDictionary *results = [jsonString JSONValue];
NSArray *photos = [[results objectForKey:#"photosets"] objectForKey:#"photoset"];
NSMutableArray *itemArray = [[NSMutableArray alloc] init];
NSMutableArray *pageArray = [[NSMutableArray alloc] init];
NSNumber *countPage = [[NSNumber alloc] initWithInt:0];
for (NSDictionary *photo in photos)
{
NSString *iconURLString = [NSString stringWithFormat:#"http://farm%#.static.flickr.com/%#/%#_%#_s.jpg",
[photo objectForKey:#"farm"], [photo objectForKey:#"server"], [photo objectForKey:#"primary"], [photo objectForKey:#"secret"]];
NSString *photoCount = [photo objectForKey:#"photos"];
NSDictionary *title = [photo objectForKey:#"title"];
NSString *itemTitle = [title objectForKey:#"_content"];
TTLauncherItem *itemMenu = [[[TTLauncherItem alloc] initWithTitle:itemTitle
image:iconURLString
URL:nil
canDelete:NO] autorelease];
itemMenu.badgeValue = photoCount;
[itemArray addObject:itemMenu];
int value = [countPage intValue];
countPage = [NSNumber numberWithInt:value + 1];
if (countPage == [NSNumber numberWithInt:9]){
countPage = [NSNumber numberWithInt:0];
[pageArray addObject:itemArray];
itemArray = [[NSMutableArray alloc] init];
}
}
[pageArray addObject:itemArray];
launcherView = [[TTLauncherView alloc] initWithFrame:self.view.bounds];
launcherView.backgroundColor = [UIColor blackColor];
launcherView.delegate = self;
launcherView.columnCount = 3;
launcherView.persistenceMode = TTLauncherPersistenceModeNone;
launcherView.pages = pageArray;
[self.view addSubview:launcherView];
}

How to add a search bar to a table view that is populated from a plist

I have tried using a couple of different search bar examples but I have not had any luck with adding a search bar. I would really appreciate any help that can be provided. The following is the code and I was using the search bar controller example.
if(CurrentLevel == 0) {
//Initialize our table data source
NSArray *tempDict = [[NSArray alloc] init];
self.tableDataSource = tempDict;
[tempDict release];
Midwest_DigestiveAppDelegate *AppDelegate = (Midwest_DigestiveAppDelegate *)[[UIApplication sharedApplication] delegate];
self.tableDataSource = [AppDelegate.data valueForKey:#"Rows"];
//Initialize the array.
NSMutableArray *listOfItems = [[NSMutableArray alloc] init];
[listOfItems addObjectsFromArray:tableDataSource];
//Initialize the copy array.
NSMutableArray *copyListOfItems = [[NSMutableArray alloc] init];
//Add the search bar
self.tableView.tableHeaderView = searchBar;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searching = NO;
letUserSelectRow = YES;
}
else
self.navigationItem.title = CurrentTitle;
//Initialize the array.
NSMutableArray *listOfItems = [[NSMutableArray alloc] initW];
[listOfItems addObjectsFromArray:tableDataSource];
//Initialize the copy array.
NSMutableArray *copyListOfItems = [[NSMutableArray alloc] init];
//Add the search bar
self.tableView.tableHeaderView = searchBar;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searching = NO;
letUserSelectRow = YES;
}
- (void) searchTableView {
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (NSDictionary *dictionary in listOfItems)
{
NSArray *array = [dictionary objectForKey:#"Title"];
[searchArray addObjectsFromArray:array];
}
for (NSString *sTemp in searchArray)
{
NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[copyListOfItems addObject:sTemp];
}
[searchArray release];
searchArray = nil;
}

Sectioned tableview problem

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:)];

Copy NSMutablearray to another

I am trying to copy NSMutableArray to another but it does not show me anything at the UITableView:
NSMutableArray *objectsToAdd= [[NSMutableArray alloc] initWithObjects:#"one",#"two"];
NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:objectsToAdd,nil];
NSMutableArray *list = [[NSMutableArray alloc] init];
[self.list addObjectsFromArray:myArray];
Nothing show up! What is wrong?
It crashes my app because I do not have nil at my NSMutableArray how can I add nil to it? addobject:nil does not work it crashes the app:
static NSString * DisclosureButtonCellIdentifier =
#"DisclosureButtonCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
DisclosureButtonCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: DisclosureButtonCellIdentifier]
autorelease];
}
NSUInteger row = [indexPath row];
NSString *rowString =nil;
rowString = [list objectAtIndex:row];
cell.textLabel.text = rowString;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
[rowString release];
return cell;
Your initial call to alloc an NSMutableArray will most likely crash, since you don't have a nil terminator in your argument list.
Also, you have a local variable, list, and a property, list. Make sure you're instantiating what you think you are. You might need to do this:
NSMutableArray *objectsToAdd= [[NSMutableArray alloc] initWithObjects:#"one",#"two", nil];
NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:objectsToAdd,nil];
self.list = [[NSMutableArray alloc] init];
[self.list addObjectsFromArray:myArray];
This might help you:
NSMutableArray *result = [NSMutableArray arrayWithArray:array];
or
NSMutableArray *result = [array mutableCopy]; //recommended
There are a few problems... One problem is that you're using 'initWithObjects' and adding the previous array. This seems like unwanted behaviour since you most likely want to add the strings #"one" and #"two" to the array. You most likely intended to use initWithArray or addObjectsFromArray. (Doing it the way you did, will add the NSMutableArray (not its objects) to the list)
The second problem, when you use initWithObjects, you need to list each of the objects and then terminate the list with a nil value. (docs) In other words, you need to use...
NSMutableArray *objectsToAdd = [[NSMutableArray alloc] initWithObjects:#"One", #"Two", nil];
The problem might be that the local declaration of listin line 4 conflicts with the property.