Tableview添加索引

来源:互联网 发布:小学生网络教学 编辑:程序博客网 时间:2024/06/08 11:49
//添加索引栏标题数组- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{           NSMutableArray *resultArray =[NSMutableArray arrayWithObject:UITableViewIndexSearch];                for (NSString *str in [[self.vpnCityListArr[0] allKeys] sortedArrayUsingComparator:^NSComparisonResult(NSString *str1, NSString *str2){            return [str1 compare:str2];        }]) {                        [resultArray addObject:str];        }                NSLog(@"索引:%@",resultArray);        return resultArray;    }//点击索引栏标题时执行- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{    //这里是为了指定索引index对应的是哪个section的,默认的话直接返回index就好。其他需要定制的就针对性处理                    if ([title isEqualToString:UITableViewIndexSearch])        {            [tableView setContentOffset:CGPointZero animated:NO];//tabview移至顶部            return NSNotFound;        }        else        {            return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index] - 1; // -1 添加了搜索标识        }        }

原创粉丝点击