我正在开发一个业务目录,其中有一个包含业务类型(美发师、机械师等)的数组和一个包含业务部分作为字母的数组(例如 A 或 B 或 C 或 D...或 Z 等) .
数据将显示在带有 A-Z 索引的表格 View 中。
我已将其设置如下,但无法想出一种方法来根据业务部分字母返回部分编号。
我想返回数组中每个部分具有相同业务类型字母(例如 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)
}
最佳答案
您可以创建字典或数组的数组。任何一种都适合您。
看看这个例子:
我有两个数组alphabets
,其中包含A、B、C...Z。另一个数组是 words
,其中包含索引[0]={apple, axe..}、index[1]={ball, bat}、index[3]={cow, cat, car , 小 bean 蔻} ....因此形成一个二维数组。
注意:这里很少有不需要的东西,你可以忽略它们,比如副标题。
- (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;
}
关于iOS-计算并返回数组中重复项的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14193750/