简体   繁体   English

如何在使用节索引时以字母顺序在tableView中填充数据

[英]How to populate data in a tableView as alphabetical order while using section index

I'm implementing an app where i need to show a tableView. 我正在实现一个需要显示tableView的应用程序。 tableView needs to have alphabetical search on the right side by clicking on any letter it needs to show data starting with that letter. tableView需要在右侧按字母顺序搜索,方法是单击它需要显示以该字母开头的数据的任何字母。 I've implemented the vertical alphabet search and when user clicks on any letter it takes user to that particular section. 我已经实现了垂直字母搜索,当用户单击任何字母时,它将带用户到该特定部分。 But the problem is every section has the same data it's not populating according to alphabet. 但是问题是每个部分都具有相同的数据,而不是根据字母填充的。

Here is my code for this. 这是我的代码。 // Here exhibitorArray contains all the info to populate data in 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;
}

output of this is coming something like this. 这样的输出是这样的。 在此处输入图片说明

The following is wrong: 以下是错误的:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.exhibitorArray count];
}

You have to separate your arrays for every section. 您必须为每个部分分隔阵列。 Let's say a different array for letter 'A' and a different array for letter 'B' and so on. 假设字母“ A”使用其他数组,字母“ B”使用其他数组,依此类推。 Then, based on the section index, you will get the data for the correct array. 然后,基于节索引,您将获得正确数组的数据。

For example you can use an array of arrays like this array[section][index] or a dictionary like this dict['A'][index] . 例如,您可以使用像array[section][index]这样的array[section][index]或像dict['A'][index]这样的dict['A'][index]

So the above function has to be: 因此,上述功能必须为:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // based on array of arrays
    return [[self.exhibitorArray objectAtIndex:indexPath.section] count];
}

and then, in your cellForRowAtIndexPath: you can use 然后,在您的cellForRowAtIndexPath:您可以使用

NSString *name_string = [[[self.exhibitorArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] valueForKey:@"name"];

Update: Convert exhibitorArray 更新:转换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]];
    }
}

The above code converts your array to a two-dimensional array based on alphabet letters. 上面的代码将您的数组转换为基于字母的二维数组。 Please feel free to change it according to your code and don't forget to handle memory deallocations if you are not using ARC. 请随时根据您的代码进行更改,如果您不使用ARC,请不要忘记处理内存释放。

You can use the method " sortedArrayUsingSelector ": 您可以使用方法“ sortedArrayUsingSelector ”:

When you got your array filled to show your data, just after: 当您填充数组以显示数据时,紧接着:

exhibitorArray=[exhibitorArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

EDIT : Your array contains Dictionarys ? 编辑 :您的数组包含Dictionarys I use this on my code. 我在我的代码上使用它。 Use the correct Key you want use to sort your objects. 使用要用于对对象进行排序的正确键。 In my case is "name": 在我的情况下是“名称”:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM