Converting table view to have sections - iphone

I have a table view, which has its data source from an array that contains names of people.
Now to make it easy to find people, I want to section the table view so that it has the letter A-Z on the right hand side, just like the Address Book app.
But my current array just contains a collection of NSStrings. How do I split them so that they are grouped by the first letter of the names? Is there any convenient way to do it?
EDIT: If anyone's interested in my final code:
NSMutableArray *arrayChars = [[NSMutableArray alloc] init];
for (char i = 'A'; i <= 'Z' ; i++) {
NSMutableDictionary *characterDict = [[NSMutableDictionary alloc]init];
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
for (int k = 0; k < [myList count]; k++) {
NSString *currentName = [[friends objectAtIndex:k] objectForKey:#"name"];
char heading = [currentName characterAtIndex:0];
heading = toupper(heading);
if (heading == i) {
[tempArray addObject:[friends objectAtIndex:k]];
}
}
[characterDict setObject:tempArray forKey:#"rowValues"];
[characterDict setObject:[NSString stringWithFormat:#"%c",i] forKey:#"headerTitle"];
[arrayChars addObject:characterDict];
[characterDict release];
[tempArray release];
}
At the end of the function I'll have:
arrayChars [0] = dictionary(headerTitle = 'A', rowValues = {"adam", "alice", etc})
arrayChars[1] = dictionary(headerTitle = 'B', rowValues = {"Bob", etc})
Thank you everyone for your help!

You can use a dictionary to sort them, so create an array with all the letters you want to sort and a array with nil objects to initialize the dictionary
NSArray *names = #[#"javier",#"juan", #"pedro", #"juan", #"diego"];
NSArray *letters = #[#"j", #"p", #"d"];
NSMutableArray *objects = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < [letters count]; ++i)
{
[objects addObject:[[NSMutableArray alloc] init]];
}
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:objects forKeys:letters];
Then you must find the first letter if the word and put that word into the corresponding key in the dictionary
for (NSString *name in names) {
NSString *firstLetter = [name substringToIndex:1];
for (NSString *letter in letters) {
if ([firstLetter isEqualToString:letter]) {
NSMutableArray *currentObjects = [dictionary objectForKey:letter];
[currentObjects addObject:name];
}
}
}
To check you can print directly the dictionary
NSLog(#"%#", dictionary);
Then is your work to fill your sections in the tableview using the dictionary

Related

How to sort NSMutableArray elements?

I have model class which contains NSString's- studentName, studentRank and studentImage. I wanna sort the NSMutableArray according to studentRanks. what I have done is
- (void)uploadFinished:(ASIHTTPRequest *)theRequest
{
NSString *response = nil;
response = [formDataRequest responseString];
NSError *jsonError = nil;
SBJsonParser *json = [[SBJsonParser new] autorelease];
NSArray *arrResponse = (NSArray *)[json objectWithString:response error:&jsonError];
if ([jsonError code]==0) {
// get the array of "results" from the feed and cast to NSArray
NSMutableArray *localObjects = [[[NSMutableArray alloc] init] autorelease];
// loop over all the results objects and print their names
int ndx;
for (ndx = 0; ndx < arrResponse.count; ndx++)
{
[localObjects addObject:(NSDictionary *)[arrResponse objectAtIndex:ndx]];
}
for (int x=0; x<[localObjects count]; x++)
{
TopStudents *object = [[[TopStudents alloc] initWithjsonResultDictionary:[localObjects objectAtIndex:x]] autorelease];
[localObjects replaceObjectAtIndex:x withObject:object];
}
topStudentsArray = [[NSMutableArray alloc] initWithArray:localObjects];
}
}
How can I sort this topStudentsArray according to the ranks scored by the Students and If the two or more student have the same rank, How can I group them.
I did like this
TopStudents *object;
NSSortDescriptor * sortByRank = [[[NSSortDescriptor alloc] initWithKey:#"studentRank" ascending:NO] autorelease];
NSArray * descriptors = [NSArray arrayWithObject:sortByRank];
NSArray * sorted = [topStudentsArray sortedArrayUsingDescriptors:descriptors];
but this is not displaying results properly. please help me to overcome this problem. thanks in advance.
doing something like this might do the trick
Initially sort the arrGroupedStudents in the (ascending/descending) order of studentRank
//Create an array to hold groups
NSMutableArray* arrGroupedStudents = [[NSMutableArray alloc] initWithCapacity:[topStudentsArray count]];
for (int i = 0; i < [topStudentsArray count]; i++)
{
//Grab first student
TopStudents* firstStudent = [topStudentsArray objectAtIndex:i];
//Create an array and add first student in this array
NSMutableArray* currentGroupArray = [[[NSMutableArray alloc] initWithCapacity:0] autorelease];
[currentGroupArray addObject:firstStudent];
//create a Flag and set to NO
BOOL flag = NO;
for (int j = i+1; j < [topStudentsArray count]; j++)
{
//Grab next student
TopStudents* nextStudent = [topStudentsArray objectAtIndex:j];
//Compare the ranks
if ([firstStudent.studentRank intValue] == [nextStudent.studentRank intValue])
{
//if they match add this to same group
[currentGroupArray addObject:nextStudent];
}
else {
//we have got our group so stop next iterations
[arrGroupedStudents addObject:currentGroupArray];
// We will assign j-1 to i
i=j-1;
flag = YES;
break;
}
}
//if entire array has students with same rank we need to add it to grouped array in the end
if (!flag) {
[arrGroupedStudents addObject:currentGroupArray];
}
}
Finally your arrGroupedStudents will contain grouped array with equal rank. I have not test run the code so you might need to fix a few things falling out of place. Hope it helps
If you want to display in the order of ranks, you should set the ascending as YES.
NSSortDescriptor * sortByRank = [[NSSortDescriptor alloc] initWithKey:#"studentRank" ascending:YES];
static int mySortFunc(NSDictionary *dico1, NSDictionary *dico2, void *context)
{
NSString *studentName1 = [dico1 objectForKey:#"studentName"];
NSString *studentName2 = [dico2 objectForKey:#"studentName"];
return [studentName1 compare:studentName2];
}
- (IBAction)sortBtnTouched:(id)sender
{
[topStudentsArray sortUsingFunction:mySortFunc context:NULL];
}

how to create an array of string or float in Objective-C

i need some help here, i need to know how to create an array of string retrieved from an array. i'm using powerplot for graph and it only accept float or string array.
i need to create something something like this dynamically.
NSString * sourceData[7] = {#"2", #"1", #"4", #"8", #"14", #"15", #"10"};
Below are my code to find out the numbers in strings.
NSInteger drunked = [appDelegate.drinksOnDayArray count];
NSMutableArray * dayArray = [[NSMutableArray alloc] init];
NSMutableArray * sdArray = [[NSMutableArray alloc] init];
//float *sdArray[7];
for (int i=0; i<drunked; i++) {
DayOfDrinks *drinksOnDay = [appDelegate.drinksOnDayArray objectAtIndex:i];
NSString * dayString= [NSDate stringForDisplayFromDateForChart:drinksOnDay.dateConsumed];
[dayArray addObject:dayString];
NSLog(#"%#",[dayArray objectAtIndex:i]);
drinksOnDay.isDetailViewHydrated = NO;
[drinksOnDay hydrateDetailViewData];
NSString * sdString= [NSString stringWithFormat:#"%#", drinksOnDay.standardDrinks];
[sdArray addObject:sdString];
NSString *tempstring;
NSLog(#"%#",[sdArray objectAtIndex:i]);
}
thanks for the help :)
Array's in Objectice-C aren't that hard to work with:
NSMutableArray *myArray = [NSMutableArray array];
[myArray addObject:#"first string"]; // same with float values
[myArray addObject:#"second string"];
[myArray addObject:#"third string"];
int i;
int count;
for (i = 0, count = [myArray count]; i < count; i = i + 1)
{
NSString *element = [myArray objectAtIndex:i];
NSLog(#"The element at index %d in the array is: %#", i, element); // just replace the %# by %d
}
You can either use NSArray or NSMutableArray - depending on your needs, they offer different functionality.
Following tutorial covers exactly what you are looking after:
http://www.cocoalab.com/?q=node/19
You can also add the elements to the array when you init (and optionally add them later only if you are using the Mutable version of a collection class:
NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:#"2", #"1", #"4", #"8", #"14", #"15", #"10", nil];
[myArray addObject:#"22"];
[myArray addObject:#"50"];
//do something
[myArray release];
You can use malloc to create a C-style array. something like this should work:
NSString **array = malloc(numElements * sizeof(NSString *))
some code here
free(array)
Be aware that unlike NSMutable array, c arrays won't do a retain, so you have to manage it if needed. And don't forget the free

Storing in NSMutableDictionaries without knowing how many there will be?

I tried looking for what I need but I did so unsuccessfully.
I am reading in an XML string from a webservice and I need to store my information into core data. I have to have it sorted starting with Department which points to all the Subdepartments in the selected Department and then when a Subdepartment is selected it will list all of the items in that Subdepartment.
The problem with this is I am receiving all my information in Data Tables so it has duplicates of departments and subdepartments.
So to sort it I want to store the information to where I have an array of Dictionaries that hold the name of the departments in each and then an array of Dictionaries that hold the name of the subdepartments in each and then another array of Dictionaries that hold all the item info.
I need it to be like the following...
Array of Department Dictionaries
Department Dictionary
String holding Department Name
Array Of Subdepartment Dictionaries
Subdepartment Dictionary
String holding Subdepartment Name
Array of Item Dictionaries
Item Dictionary
String holding Item Name
String holding more Item Info
But the problem is I don't know how to make this happen so that when I first encounter a Department I save it and then any Subdepartment of that department I encounter later on can just be stored in the array of that Departments dictionary and that any item of a subdepartment would so the same thing. Can I even do this?
(sorry its not pretty I can't figure out how to put it any other way...)
XML Code Example:
<QSR_VIEWS_INVENTORY_ITEMS_LIST diffgr:id="QSR_VIEWS_INVENTORY_ITEMS_LIST1" msdata:rowOrder="0"><CompanyID>104</CompanyID><QSRInventoryItemID>111</QSRInventoryItemID><Description>Test Item 111</Description><Department>_</Department><Subdepartment>_</Subdepartment><SequenceNumber>0</SequenceNumber><CountDisplayUnitName>CA</CountDisplayUnitName><CountDisplayUnitInCase>1.0000</CountDisplayUnitInCase><ReorderAt>0.0000</ReorderAt><ReorderTo>0.0000</ReorderTo><CaseUnitName>CA</CaseUnitName><CaseInCase>1.0000</CaseInCase><PackUnitName>_</PackUnitName><PacksInCase>0.0000</PacksInCase><StackUnitName>_</StackUnitName><StacksInCase>0.0000</StacksInCase><EachUnitName>_</EachUnitName><EachInCase>0.0000</EachInCase><InLocation1>N</InLocation1><InLocation2>N</InLocation2><InLocation3>N</InLocation3><InLocation4>N</InLocation4><InLocation5>N</InLocation5><InLocation6>N</InLocation6><InLocation7>N</InLocation7><InLocation8>N</InLocation8><InLocation9>N</InLocation9><InLocation10>N</InLocation10><InLocation11>N</InLocation11><InLocation12>N</InLocation12><InLocation13>N</InLocation13><InLocation14>N</InLocation14><InLocation15>N</InLocation15><OnShiftCountSheet>Y</OnShiftCountSheet><OnDayCountSheet>Y</OnDayCountSheet><OnWeekCountSheet>Y</OnWeekCountSheet><OnMonthCountSheet>Y</OnMonthCountSheet><OnWasteCountSheet>Y</OnWasteCountSheet><EquivalentToItemID>0</EquivalentToItemID><EquivalentCaseFactor>0.0000</EquivalentCaseFactor></QSR_VIEWS_INVENTORY_ITEMS_LIST>
I think all you need is one extra dictionary which has a key for all of the individual departments and sub departments, then you use that dictionary to check if you already have the new department or not, and if you don't, create it in the appropriate place. The memory overhead should be negligible, the extra dictionary will just have pointers to existing objects.
Answer provided by Alex Mayfield:
NOTE: There are probably way better ways to do this than what I have done.
BOOL addI = YES;
BOOL addS = YES;
BOOL addD = YES;
int I;
int S;
int D;
for(int i = 0; i < [DArray count]; i++)
{
DDictionary = [DArray objectAtIndex:i];
//NSLog([NSString stringWithFormat:#"%# vs %#", soapResults12, [DDictionary objectForKey:#"Title"]]);
if([soapResults12 isEqualToString:[DDictionary objectForKey:#"Title"]])
{
D = i;
//NSLog(#"GOT IT");
SArray = [DDictionary objectForKey:#"Subdepartments"];
for(int k = 0; k < [SArray count]; k++)
{
SDictionary = [SArray objectAtIndex:k];
soapResults11 = [soapResults11 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
//NSLog([NSString stringWithFormat:#"%# vs %#", soapResults11, [SDictionary objectForKey:#"Title"]]);
if([soapResults11 isEqualToString:[SDictionary objectForKey:#"Title"]])
{
S = k;
//NSLog(#"GOT IT");
IArray = [SDictionary objectForKey:#"Items"];
for(int r = 0; r < [IArray count]; r++)
{
IDictionary = [IArray objectAtIndex:r];
//NSLog([NSString stringWithFormat:#"%# vs %#", soapResults1, [IDictionary objectForKey:#"Title"]]);
if([soapResults1 isEqualToString: [IDictionary objectForKey:#"Title"]])
{
//NSLog(#"GOT IT");
I = r;
r = [IArray count];
addI = NO;
addS = NO;
addD = NO;
}
else
{
addI = YES;
addS = NO;
addD = NO;
}
}
k = [SArray count];
}
else
{
addS = YES;
addD = NO;
}
}
i = [DArray count];
}
else
{
addD = YES;
}
}
if(addI && !addS && !addD)
{
IDictionary = [[NSMutableDictionary alloc] init];
[IDictionary setObject:soapResults1 forKey:#"Title"];
[IArray addObject:IDictionary];
[SDictionary setObject:IArray forKey:#"Items"];
[SArray replaceObjectAtIndex:S withObject:SDictionary];
[DDictionary setObject:SArray forKey:#"Subdepartments"];
[DArray replaceObjectAtIndex:D withObject:DDictionary];
}
if(addS && !addD)
{
IDictionary = [[NSMutableDictionary alloc] init];
IArray = [[NSMutableArray alloc] init];
[IDictionary setObject:soapResults1 forKey:#"Title"];
[IArray addObject:IDictionary];
SDictionary = [[NSMutableDictionary alloc] init];
[SDictionary setObject:soapResults11 forKey:#"Title"];
[SDictionary setObject:IArray forKey:#"Items"];
[SArray addObject:SDictionary];
[DDictionary setObject:SArray forKey:#"Subdepartments"];
[DArray replaceObjectAtIndex:D withObject:DDictionary];
}
if(addD)
{
IDictionary = [[NSMutableDictionary alloc] init];
IArray = [[NSMutableArray alloc] init];
[IDictionary setObject:soapResults1 forKey:#"Title"];
[IArray addObject:IDictionary];
SDictionary = [[NSMutableDictionary alloc] init];
SArray = [[NSMutableArray alloc] init];
[SDictionary setObject:soapResults11 forKey:#"Title"];
[SDictionary setObject:IArray forKey:#"Items"];
[SArray addObject:SDictionary];
DDictionary = [[NSMutableDictionary alloc] init];
[DDictionary setObject:soapResults12 forKey:#"Title"];
[DDictionary setObject:SArray forKey:#"Subdepartments"];
[DArray addObject:DDictionary];
}

how to create an C Array

his guys,
i think this is a simple question but i do not know how to do it.
how do i create the line below dynamically from an array?
this is what i need to call.
//data source
NSString * sourceData[7] = {#"2", #"1", #"4", #"8", #"14", #"15", #"10"};
chartData = [WSData dataWithValues:[WSData arrayWithString:sourceData withLen:7]];
+ (NSArray *)arrayWithString:(NSString *[])strings
withLen:(NSUInteger)len {
NSMutableArray *tmpArr = [NSMutableArray
arrayWithCapacity:len];
NSUInteger i;
for (i=0; i<len; i++) {
[tmpArr addObject:strings[i]];
}
return [NSArray arrayWithArray:tmpArr];
}
thanks for all the help especially Daniel :)
this is the answer to the question
NSMutableArray * dayArray = [[NSMutableArray alloc] init];
dayArray = [NSMutableArray arrayWithCapacity:7];
NSMutableArray * sdArray = [[NSMutableArray alloc] init];
sdArray = [NSMutableArray arrayWithCapacity:7];
NSInteger drunked = [appDelegate.drinksOnDayArray count];
if (drunked !=0)
{
for(int i=6; i>=0; i--)
{
DayOfDrinks *drinksOnDay = [appDelegate.drinksOnDayArray objectAtIndex:i];
NSString * dayString= [NSDate stringForDisplayFromDateForChart:drinksOnDay.dateConsumed];
[dayArray addObject:dayString];//X label for graph the day of drink.
drinksOnDay.isDetailViewHydrated = NO;
[drinksOnDay hydrateDetailViewData];
NSNumber *sdNumber = drinksOnDay.standardDrinks;
[sdArray addObject: sdNumber];
}
NSString *sData[7];// = malloc(7 * sizeof(NSString *));
for (int i=0; i<7; i++)
{
DayOfDrinks *drinksOnDay = [appDelegate.drinksOnDayArray objectAtIndex:i];
sData[i] = [NSString stringWithFormat:#"%#",drinksOnDay.standardDrinks];
}
NSString * sourceData[7] = {sData[6],sData[5],sData[4],sData[3],sData[2],sData[1],sData[0] };
}
If you are only using the array as a call parm, and are not storing it somewhere or returning it from your current method:
NSString* sourceData[7];
for (i = 0; i < 7; i++) {
int num = <getTheValueYouWant>;
sourceData[i] = [NSString stringWithFormat:#"%d", num];
}
But note that if you intend to return the array, or store it in some long-lived variable, you need an entirely different setup.
So this is how you would create an NSMutableArray. Mutable because you're creating it at runtime.
NSString * sourceData = [[NSString alloc] initWithFormat:#""];
//assuming the array you have is arr with NSNumber objects
for (NSNumber *num in arr) {
[sourceData stringByAppendingFormat:#"%#", num];
}
You can try below code with the loop as you required.....
NSMutableArray *array;
array = [[NSMutableArray alloc] init];
[array addObject:[NSNumber numberWithFloat:1.0f]];
[array release];

Problem adding custom objects to Mutable Array

quick question regarding Array's in xcode. I have ht efollowing code, which is supposed to go through an array of strings which it has got through php and JSON, and trun these strings into a custom object with the strings as the ivars for the object then add that object to a new array:
for (int i = 0; i<[list count]; i++) {
Article *article = [[Article alloc] init]; //creates custom object
article.uid = [[list objectAtIndex:i] objectAtIndex:0];
article.title = [[list objectAtIndex:i] objectAtIndex:1]; //adds string as ivars
article.description = [[list objectAtIndex:i] objectAtIndex:2];
articleArray = [[NSMutableArray alloc] init]; //inits the new array
[articleArray addObject:article]; //should add the object but seems to fail
[article release]; //releases the object
NSLog(#"%#", article.description);
}
NSLog(#"%d", [articleArray count]);
NSLog([articleArray description]);
}
The code does return the correct values using NSLog(#"%#", article.description); but not the correct length for the new array and it only adds one value to the array which is the string for article.description which makes no sense to me. The list array contains 2 elements each of which are arrays in themselves containing the strings.
You're recreating the articleArray in every loop. Declarate it outside, and it will work:
NSMutableArray *articleArray = [[NSMutableArray alloc] init]; //inits the new array
for (int i = 0; i<[list count]; i++) {
Article *article = [[Article alloc] init]; //creates custom object
article.uid = [[list objectAtIndex:i] objectAtIndex:0];
article.title = [[list objectAtIndex:i] objectAtIndex:1]; //adds string as ivars
article.description = [[list objectAtIndex:i] objectAtIndex:2];
[articleArray addObject:article]; //should add the object but seems to fail
[article release]; //releases the object
NSLog(#"%#", article.description);
}
NSLog(#"%d", [articleArray count]);
NSLog([articleArray description]);
}
You also may want to use the nicer for(NSArray *listElement in list) syntax instead.