Reputation: 809
There's probably a straight forward answer to this but I can't find one.
I have an NSArray, filled with NSDictionary objects. The number of objects/2 within each NSDictionary object determines the number of rows in each section of a UITableviewcell. Likewise, the number of NSDictionary objects, determines the number of sections - (that's simply [NSArray count]).
A basic example:
self.myArray = [NSArray arrayWithObects:
[NSDictionary dictionaryWithObjectsAndKeys:obj1, key1, obj2, key2,..., nil],
...
[NSDictionary dictionaryWithObjectsAndKeys:objA, keyA ... objn, keyn, nil],
nil];
What I have been doing is manually counting the number of objects in each and every NSDictionary, dividing each number by 2 and using that information to construct an array where each element contains the number of rows per section. That manually constructed array is then used in the tableview numberOfRowsInSection delegate. This seems crude at best. Incidentally, but not important, the reason I divide by 2 is that each tableview cell is composed with 2 UIlabel subviews - 1 label for obj..., the other for key..., for each set of NSDictionary objects within.
A big thanks in advance.
Upvotes: 0
Views: 848
Reputation: 16861
@implementation NSArray (totalObjects)
- (NSUInteger) totalObjects
{
NSUInteger result = 0;
for (id object in self)
result += [object isKindOfClass:[NSDictionary class]] ? [[object allKeys] count] : 1;
return result;
}
@end
Upvotes: 1
Reputation: 8515
Use the count
function on NSArray and NSDictionary:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [myArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSDictionary *dictionary = [myArray objectAtIndex:section];
return [dictionary count] / 2;
}
Upvotes: 1