Reputation: 37
I am developing a Business directory where I have an array containing business types (Hairdresser, mechanic, etc) and an array containing business sections as letters (Eg. A or B or C or D... or Z, etc).
The data will be shown in a tableview with an A-Z index.
I have set it up as follows but can't think of a way to return the section numbers based on the business section letter.
I want to return the number of objects in the array that have the same business type letter (Eg. A or B or C or D... or Z, etc) in each section.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 26;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
[tempArray addObject:@"A"];
[tempArray addObject:@"B"];
[tempArray addObject:@"C"];
[tempArray addObject:@"D"];
[tempArray addObject:@"E"];
[tempArray addObject:@"F"];
[tempArray addObject:@"G"];
[tempArray addObject:@"H"];
[tempArray addObject:@"I"];
[tempArray addObject:@"J"];
[tempArray addObject:@"K"];
[tempArray addObject:@"L"];
[tempArray addObject:@"M"];
[tempArray addObject:@"N"];
[tempArray addObject:@"O"];
[tempArray addObject:@"P"];
[tempArray addObject:@"Q"];
[tempArray addObject:@"R"];
[tempArray addObject:@"S"];
[tempArray addObject:@"T"];
[tempArray addObject:@"U"];
[tempArray addObject:@"V"];
[tempArray addObject:@"W"];
[tempArray addObject:@"X"];
[tempArray addObject:@"Y"];
[tempArray addObject:@"Z"];
return tempArray;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// I want to return the number of objects in the array that have the same section name (EG. A or B or C etc)
}
Upvotes: 0
Views: 238
Reputation: 46543
You can create a Dictionary or an Array of array. Either one will work for you.
Just see this example :
I have two arrays alphabets
which contains A, B, C...Z. And other array is words
which contains at index[0]={apple, axe..}, index[1]={ball, bat}, index[3]={cow, cat, car, cardamom}.... So forming a two-Dimentional array.
NOTE: Few unwanted things are here, you can ommit them, like subtitle.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.words count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [[self.words objectAtIndex:section]count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
cell.textLabel.text=[[self.words objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[NSString stringWithFormat:@"This is detailed subtitle for %@.",cell.textLabel.text];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [self.alphabets objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;{
return self.alphabets;
}
Upvotes: 1
Reputation: 56635
You can create a NSCountedSet
from the original array that knows how many times each object occures
NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:yourArray];
Then you can get the count for each object using countForObject:
on the set.
If the order matters to you then you can enumerate over the array and call countForObject:
on the set with the object from the array.
Upvotes: 0