[英]iOS How to populate the Data in Tableview Header and Section in swift?
[英]How to populate data in a tableView as alphabetical order while using section index
我正在实现一个需要显示tableView的应用程序。 tableView需要在右侧按字母顺序搜索,方法是单击它需要显示以该字母开头的数据的任何字母。 我已经实现了垂直字母搜索,当用户单击任何字母时,它将带用户到该特定部分。 但是问题是每个部分都具有相同的数据,而不是根据字母填充的。
这是我的代码。 //在这里, 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]
这样的array[section][index]
或像dict['A'][index]
这样的dict['A'][index]
。
因此,上述功能必须为:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// based on array of arrays
return [[self.exhibitorArray objectAtIndex:indexPath.section] count];
}
然后,在您的cellForRowAtIndexPath:
您可以使用
NSString *name_string = [[[self.exhibitorArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] valueForKey:@"name"];
更新:转换ExhibitorArray
- (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]];
}
}
上面的代码将您的数组转换为基于字母的二维数组。 请随时根据您的代码进行更改,如果您不使用ARC,请不要忘记处理内存释放。
您可以使用方法“ sortedArrayUsingSelector
”:
当您填充数组以显示数据时,紧接着:
exhibitorArray=[exhibitorArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
编辑 :您的数组包含Dictionarys
? 我在我的代码上使用它。 使用要用于对对象进行排序的正确键。 在我的情况下是“名称”:
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:descriptor, nil];
exhibitorArray= [exhibitorArray sortedArrayUsingDescriptors:sortDescriptors];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.