我正在开发一个Business目录,其中我有一个包含业务类型(美发师,机械师等)的数组和一个包含业务部分作为字母的数组(例如A或B或C或D ......或Z等)
数据将显示在带有A-Z索引的tableview中。
我已将其设置如下,但无法想出根据业务部分信函返回部分编号的方法。
我想在每个部分中返回数组中具有相同业务类型字母的对象数(例如,A或B或C或D ......或Z等)。
- (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)
}
答案 0 :(得分:1)
您可以创建一个Dictionary或一个数组Array。任何一个都适合你。
请看这个例子:
我有两个数组alphabets
,其中包含A,B,C ...... Z.其他数组是words
,其中包含索引[0] = {apple,ax ..},index [1] = {ball,bat},index [3] = {cow,cat,car,cardamom} ....所以形成一个二维数组。
注意:这里很少有不需要的东西,你可以省略它们,比如副标题。
- (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;
}
答案 1 :(得分:0)
您可以从原始数组创建一个NSCountedSet
,知道每个对象发生的次数
NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:yourArray];
然后,您可以使用集合上的countForObject:
获取每个对象的计数。
如果订单对您很重要,那么您可以枚举数组并使用数组中的对象调用集合上的countForObject:
。