簡體   English   中英

如何在使用節索引時以字母順序在tableView中填充數據

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM