Ios 如何在使用节索引时按字母顺序填充tableView中的数据
我正在实现一个需要显示tableView的应用程序。tableView需要在右边按字母顺序搜索,方法是单击需要的任何字母,以显示以该字母开头的数据。我已经实现了垂直字母表搜索,当用户单击任何字母时,它会将用户带到该特定部分。但问题是每个部分都有相同的数据,它不是按照字母表填充的 这是我的代码。 //此处Ios 如何在使用节索引时按字母顺序填充tableView中的数据,ios,iphone,objective-c,uitableview,Ios,Iphone,Objective C,Uitableview,我正在实现一个需要显示tableView的应用程序。tableView需要在右边按字母顺序搜索,方法是单击需要的任何字母,以显示以该字母开头的数据。我已经实现了垂直字母表搜索,当用户单击任何字母时,它会将用户带到该特定部分。但问题是每个部分都有相同的数据,它不是按照字母表填充的 这是我的代码。 //此处exhibitorArray包含用于在tableView中填充数据的所有信息 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tab
exhibitorArray
包含用于在tableView中填充数据的所有信息
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.exhibitorArray count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.exhibitorArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
[self.exhibitorTableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
bgview.opaque = YES;
bgview.backgroundColor = [UIColor colorWithRed:244.0f/255 green:245.0f/255 blue:246.0f/255 alpha:1.0];
[cell setBackgroundView:bgview];
UIImageView *defaultImage = [[UIImageView alloc]init];
[defaultImage setFrame:CGRectMake(5.0f, 5.0f, 40.0f, 40.0f)];
[cell addSubview:defaultImage];
NSString *urlString = @"http://";
urlString = [urlString stringByAppendingString:[NSString stringWithFormat:@"%@",[[self.exhibitorArray objectAtIndex:indexPath.row]valueForKey:@"image"]]];
NSLog(@"%@",urlString);
AsyncImageView *asyncImageView = [[AsyncImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 40.0f, 40.0f)];
[asyncImageView setBackgroundColor:[UIColor clearColor]];
[asyncImageView loadImageFromURL:[NSURL URLWithString:urlString]];
[defaultImage addSubview:asyncImageView];
defaultImage.contentMode = UIViewContentModeScaleAspectFit;
asyncImageView = nil;
defaultImage = nil;
NSString *name_string = [[self.exhibitorArray objectAtIndex:indexPath.row]valueForKey:@"name"];
UILabel *user_name = [[ UILabel alloc]init ];
[user_name setFrame:(CGRectMake(58, 5, 270, 25))];
[user_name setBackgroundColor: [UIColor clearColor]];
[user_name setText:name_string];
[user_name setFont:[UIFont fontWithName:@"Roboto-Light" size:14]];
[user_name setTextAlignment:NSTextAlignmentLeft];
[user_name setTextColor:[UIColor colorWithRed:93.0f/255 green:94.0f/255 blue:94.0f/255 alpha:1.0]];
[cell addSubview:user_name];
user_name = nil;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
这个的输出是这样的。
以下是错误的:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.exhibitorArray count];
}
您必须为每个部分分离阵列。假设字母“a”有一个不同的数组,字母“B”有一个不同的数组,以此类推。然后,根据节索引,您将获得正确数组的数据
例如,您可以使用这样的数组array[section][index]
或这样的字典dict['a'][index]
因此,上述功能必须是:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// based on array of arrays
return [[self.exhibitorArray objectAtIndex:indexPath.section] count];
}
然后,在单元格中,您可以使用
NSString *name_string = [[[self.exhibitorArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] valueForKey:@"name"];
更新:转换参展商阵列
- (void)convertArray
{
//NSMutableArray *exhibitorArray = [[NSMutableArray alloc] initWithArray:@[@"abra", @"Catabra"]];
NSArray *data = [exhibitorArray copy];
exhibitorArray = [[NSMutableArray alloc] initWithCapacity:27];
// 27 elements, 26 for A-Z plus one for '#'
for (int i=0; i<27; i++)
[exhibitorArray addObject:[[NSMutableArray alloc] init]];
int firstAsciiPos = (int)[@"A" characterAtIndex:0];
for (int i=0; i<[data count]; i++)
{
int index = (int)[[[data objectAtIndex:i] uppercaseString] characterAtIndex:0] - firstAsciiPos;
if (index < 0 || index > 25)
{
// it is not between A and Z, add it to '#'
index = 26;
}
[[exhibitorArray objectAtIndex:index] addObject:[data objectAtIndex:i]];
}
}
-(void)转换数组
{
//NSMutableArray*exhibitorArray=[[NSMutableArray alloc]initWithArray:@[@“abra”,“Catabra]”;
NSArray*数据=[参展商阵列副本];
参展商阵列=[[NSMutableArray alloc]初始容量:27];
//27个元素,26个表示A-Z,另一个表示“#”
对于(int i=0;i您可以使用方法“sortedarrayingselector
”:
当您填充数组以显示数据时,就在:
exhibitorArray=[exhibitorArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
编辑:
您的数组包含词典
?我在我的代码中使用此项。请使用您希望用于对对象排序的正确键。在我的示例中是“name”:
请显示cellForRowAtIndextPathsure..编辑了我的问题,您现在可以检查。看,我试图理解这一点,但无法清楚地理解当我使用此-(NSInteger)tableView:(UITableView*)tableView行数section:(NSInteger)section{//基于数组返回[[self.exhibitorArray objectAtIndex:section]objectAtIndex:row];}
它给了我一个类似于“使用未声明的标识符行”的错误你想让我将行声明为独立的东西。我的错误是,在我编辑的答案中,你必须询问数组的计数。很明显,但值得一提的是,你必须在开始填充tableview单元格之前对数据进行结构化。当你说我必须在填充之前分离数据结构时。你的意思是什么。我必须我认为我在为我的数据创建合适的数组时出错了,所以我建议你创建一个包含26个元素的数组(作为字母表的长度)。每个元素都是一个数组。例如,exhibitorArray的第一个元素将包含一个数组,其中所有元素都从“A”开始。然后,exhibitorArray的第二个元素将是一个数组,可以说是零元素。这意味着您没有从“B”开始的记录。依此类推。将数据相应地推送到不同的数组基于首字母的ys。我尝试过,但还没有成功,你能帮我编写一些代码吗。实际上我以前也尝试过。NSSortDescriptor*nameSorter=[[NSSortDescriptor alloc]initWithKey:@“name”升序:是选择器:@selector(caseInsensitiveCompare:)];[self.exhibitorray SortusingDescriptor:[NSArray arrayWithObject:nameSorter]];self.dataList2=self.exhibitorArray`
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:descriptor, nil];
exhibitorArray= [exhibitorArray sortedArrayUsingDescriptors:sortDescriptors];