gpt4 book ai didi

iphone - 使 UITableView 可按索引滚动

转载 作者:IT王子 更新时间:2023-10-29 08:13:12 25 4
gpt4 key购买 nike

我正在尝试创建一个 UITableView 索引以更快地滚动大型 UITableView,我在右侧显示了索引,但是当我尝试使用它滚动时, 它不起作用..

screenshot

我想知道我是否遗漏了什么,所以我想知道是否还有其他事情我必须做,或者类似的事情。

#pragma - TableView Index Scrolling

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

if(searching)
return nil;

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:@"Y"];
[tempArray addObject:@"X"];
[tempArray addObject:@"Z"];

return tempArray;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {

if(searching)
return -1;

return index % 2;
}

最佳答案

你可以用这个

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index

它告诉表格哪个部分对应于部分标题/索引(例如“B”,1))

此方法用于返回与标题对应的部分。但是您想根据这些标题滚动单个部分的行。所以请按照以下步骤操作:

  1. 创建全局数组意味着在.h文件中声明它

    NSMutableArray *alphabetsArray;
    NSMutableArray *dataArray; //--This contain your cell value i.e. Honda,Mazda--//
  2. viewDidLoad 方法中定义它,如下所示,然后从 UITableView 方法返回它..

    alphabetsArray = [[NSMutableArray alloc] init];
    [alphabetsArray addObject:@"A"];
    [alphabetsArray addObject:@"B"];
    [alphabetsArray addObject:@"C"];
    [alphabetsArray addObject:@"D"];
    [alphabetsArray addObject:@"E"];
    [alphabetsArray addObject:@"F"];
    [alphabetsArray addObject:@"G"];
    [alphabetsArray addObject:@"H"];
    [alphabetsArray addObject:@"I"];
    [alphabetsArray addObject:@"J"];
    [alphabetsArray addObject:@"K"];
    [alphabetsArray addObject:@"L"];
    [alphabetsArray addObject:@"M"];
    [alphabetsArray addObject:@"N"];
    [alphabetsArray addObject:@"O"];
    [alphabetsArray addObject:@"P"];
    [alphabetsArray addObject:@"Q"];
    [alphabetsArray addObject:@"R"];
    [alphabetsArray addObject:@"S"];
    [alphabetsArray addObject:@"T"];
    [alphabetsArray addObject:@"U"];
    [alphabetsArray addObject:@"V"];
    [alphabetsArray addObject:@"W"];
    [alphabetsArray addObject:@"Y"];
    [alphabetsArray addObject:@"X"];
    [alphabetsArray addObject:@"Z"];

    [Note: You can also create alphabetsArray with dynamic contains according to your dataArray. For this follow How we can create dynamic section index titles array at below]

  3. 现在使用上面提到的方法获取标题

    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return alphabetsArray;
    }
  4. 编写滚动 UITableView 的逻辑

    - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    for (int i = 0; i< [dataArray count]; i++) {
    // Here you return the name i.e. Honda,Mazda
    // and match the title for first letter of name
    // and move to that row corresponding to that indexpath as below
    NSString *letterString = [[dataArray objectAtIndex:i] substringToIndex:1];
    if ([letterString isEqualToString:title]) {
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    break;
    }
    }
    return -1;
    }

****我们如何创建动态章节索引标题数组****

  1. 让我们假设 dataArray 是这样的

    [@"Honda",@"Maruti",@"Mazda",@"Toyota",.....]  
  2. 如下创建动态章节索引标题数组的方法

    - (void)createAlphabetArray;
  3. 如下实现createAlphabetArray方法

    #pragma mark - Create Alphabet Array
    - (void)createAlphabetArray {
    NSMutableArray *tempFirstLetterArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < [dataArray count]; i++) {
    NSString *letterString = [[dataArray objectAtIndex:i] substringToIndex:1];
    if (![tempFirstLetterArray containsObject:letterString]) {
    [tempFirstLetterArray addObject:letterString];
    }
    }
    alphabetsArray = tempFirstLetterArray;
    [tempFirstLetterArray release];
    }
  4. 现在只要你想改变 alphabetsArray 包含的任何地方就可以调用它

    [self createAlphabetArray]; 

现在您可以根据您的 dataArray 内容动态创建动态章节索引标题数组。

关于iphone - 使 UITableView 可按索引滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7408474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com