0

My aim is to produce an array, which I can use to add section headers for a UITableView. I think the easiest way to do this, is to produce a sections array.

I want to create section headers for dates, where I'll have several or no rows for each.

So in my populate data array function, I want to populate a display array. So record 1, look for the first date in my display array, create a new array item if it doesn't exist, if it does exist add 1 to the count.

So I should end up with something like this.

arrDisplay(0).description = 1/June/2001; arrDisplay(0).value = 3;
arrDisplay(1).description = 2/June/2001; arrDisplay(1).value = 0;
arrDisplay(2).description = 3/June/2001; arrDisplay(2).value = 1;
arrDisplay(3).description = 5/June/2001; arrDisplay(3).value = 6;

My question is how do I create and use such an array with values, where I can add new elements of add to the count of existing elements and search for existing elements ?

4 Answers 4

1

I think, if i understand you, an NSMutableDictionary would work. (as NR4TR said) but, i think the object would be the description and the key would be the count. you could check for the key and get the count in the same gesture. if the return value of objectForKey is nil, it doesn't exist.

NSMutableDictionary *tableDictionary = [[NSMutableDictionary alloc] init];

NSString *displayKey = @"1/June/2001";

NSNumber *displayCount = [tableDictionary objectForKey:displayKey];

if (displayCount != nil) {
 NSNumber *incrementedCount = [[NSNumber alloc] initWithInteger:[displayCount integerValue] + 1];
 [tableDictionary removeObjectForKey:displayKey];
 [tableDictionary setValue:incrementedCount
      forKey:displayKey];
 [incrementedCount release];
}
else {
 NSNumber *initialCount = [[NSNumber alloc] initWithInteger:1];
 [tableDictionary setValue:initialCount
      forKey:displayKey];
 [initialCount release];
}

EDIT: Hopefully this isn't pedantic, but I think a couple pointers will help.

Dictionaries, Sets, and Arrays all hold objects for retrieval. The manner of holding and retrieval desired drives the decision. I think of it based on the question 'what is the nature of the information that I have when I need an object being held?'

NSDictionary and NSMutableDictionary

  • Hold n objects per key. (I think...I haven't had to test a limit, but i know you can get an NSSet back as a value.)
  • KEY is more important than INDEX. I don't think of dictionaries as ordered. they know something and you need to ask the correct question.

NSArray and NSMutableArray

  • hold n objects in order.
  • INDEX is most important bit of information. (you can ask for the index of an object but, even here, the index is the important part)
  • you will typically drive table views with an array because the ordered nature of the array fits.

NSSet, NSMutableSet, and NSCountedSet

  • A collection of objects without order.

You can change any of these into the other with something like [nsset setFromArray:myArray];

and all of these things can hold the other as objects. I think an array as your top level is the correct thinking, but beyond that, it becomes an issue of implementation

Sign up to request clarification or add additional context in comments.

4 Comments

Cool, how can I get a count by index ? also the key by index ? Thanks.
What are all of the things you need to keep track of? I am slightly confused now and I think seeing some of your code would help. I think 2 dictionaries or 2 arrays would handle your issue.
and you can usually get count for a collection of objects with [myDictionary count];
NSNumber *displayCount = [tableDictionary objectForKey:displayKey]; is where i get the count. i set in the if/else statement
1

Try array of dictionaries. Each dictionary contains two objects - section title and array of section rows.

2 Comments

What about the search and add ?
Regarding search, please investigate developer.apple.com/library/ios/#samplecode/TableSearch. As for adding, you just should manipulate with that magic array of dictionaries. It could be tricky if you don't have enough imagination :)
0

If you want to have a description AND a rowcount then you can either create a class with those two properties and generate an NSArray of objects with that class or instead of all that you can just use an NSDictionary to store key/value lookups.

2 Comments

How do i do a look up, have you an example ?
0

I think NSCountedSet is closest to what you want. It doesn't have an intrinsic order, but you can get an array out of it by providing a sort order.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.