objective-c - 具有多个部分时使用 sectionIndexTitlesForTableView 进行索引

标签 objective-c ios uitableview uiscrollview indexing

我有一个表格 View ,我在其中使用 sectionIndexTitlesForTableView 来显示索引。我必须显示位于不同国家/地区的大学列表。每个国家可以有不止一所大学,所以表格显示如下:

 1. Ecuador
    1. Universidad Catolica del Ecuador
 2. El Salvador
    1. Universidad Don Bosco
 3. Estonia
    4. Tallin University
    4. Tartu Health Care College
    4. University of Tartu
 4. Finland
    5. Aalto University

....

我打算做的是,使用侧面的滚动搜索,当用户触摸“A”时,它会将您带到第一个带有字母 A 的条目。例如,当我按“E”时,它应该我到“厄瓜多尔”部分(显示在上面的“图像”上)。

问题一开始就出现了,因为我有多个部分,当我按“B”时,它将带我到“澳大利亚”,第二部分(第一个是阿根廷)而不是带我到“比利时”,这将是第一个带有 B 字母的国家。我设法解决了这个问题,但它给了我错误。这是代码:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
   return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}


- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    NSArray* bu =[self SectionsASaltar];
    int ins =[[bu objectAtIndex:index] intValue];

    return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:ins];

}


-(NSArray*)SectionsASaltar
{
    NSArray* ALF= [[NSArray alloc] initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z", nil];


    NSMutableArray* jumps =[[NSMutableArray alloc]initWithObjects:@"0", nil];

    int i=0; //contador de elementos
    int j=0; //contador del alfabeto

    [titl addObject:[self.secc objectAtIndex:i]];
    do {

        if ([[self.secc objectAtIndex:i] hasPrefix:[ALF objectAtIndex:j]]) {
            i++;
        }else{
            [jumps addObject:[NSString stringWithFormat:@"%d",i]];
            i++;
            j++;

        }

    } while (i<[self.secc count]);

    [jumps addObject:[NSString stringWithFormat:@"0"]];
    NSLog(@"%@",jumps);
    return jumps;
}

其中 self.secc 是我的包含部分的数组,jumps 是一个数组,其中包含我必须在索引上跳过才能到达下一个字母的元素数。

上面代码的思路是计算有多少个元素有字母A,存储它并在用户按下B时跳过它们,所以它会直接转到第一个字母B。它适用于大多数字母但是一旦我按下字母 M,就会出现以下错误:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (29) beyond bounds (28)'
*** First throw call stack:
(0x25f9012 0x21c6e7e 0x25f8deb 0x25ed7e0 0x13137be 0x1dcd2 0x10d27ee 0x21da705 0x102e920 0x102e8b8 0x10ef671 0x10efbcf 0x124396d 0x12439a6 0x10ee7e2 0x105e16d 0x105e552 0x103c3aa 0x102dcf8 0x32b3df9 0x32b3ad0 0x256ebf5 0x256e962 0x259fbb6 0x259ef44 0x259ee1b 0x32b27e3 0x32b2668 0x102b65c 0x2bbd 0x2af5 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb)

我真的不知道我在哪里越界了,我认为它在行中:

return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:ins];

但我不知道该语句是如何工作的,所以我不知道如何修复它。有任何想法吗? 此外,这是我编制索引的最佳方法,还是有更简单的方法来管理相同字母的部分。我知道我可以做出另一种观点,然后让第一种观点只有按字母排序的国家,第二种观点是大学,但我正在努力避免这种情况。

最佳答案

您的代码假定字母表中的每个字母都有一个条目。如果不是,那么它将递增 i 和 J,跳过事件条目。因此,如果您有一个以字母 a、c、d、e 开头的数组,那么您的代码将用 a 测试 a,然后用 b 测试 c,递增两个索引器,在下一个循环中它将用 c 而不是 c 测试 d与c。因为你不断递增 j 你可以超越你的项目索引。

关于objective-c - 具有多个部分时使用 sectionIndexTitlesForTableView 进行索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14207186/

相关文章:

ios - 如何为 iPhone 5s 和 6s plus 设计?

ios - CBPeripheralManager : Does add(_ service: CBMutableService) advertise peripheral?

objective-c - NSSet 使用谓词返回匹配给定类的对象

ios - AVPlayer seekToTime : backward not working

ios - 核心蓝牙的问题

ios - 如何在 Realm 中移动对象

iphone - UITableView子菜单

iphone - 将 UIImage 添加到 UITableViewCell "State Touched"

objective-c - 如何结束使用 detachNewThreadSelector 创建的轮询线程?

iphone - 当 tableview 比屏幕长时将其转换为图像