为UITableView加入索引

来源:互联网 发布:淘宝oem化妆品 编辑:程序博客网 时间:2024/06/09 20:15

//设置索引

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

{

    if (tableView == myTableView) {

        

        NSMutableArray *charArray = [[NSMutableArray alloc]initWithCapacity:0];

        //加一个搜索索引

        [charArray addObject:UITableViewIndexSearch];

        

        [charArray addObject:@"中国"];

        [charArray addObject:@"外国"];

        

        return charArray;

    }

    return nil;

}


//解决加入索引图标混乱


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

{

    if (index == 0) {

        [tableView scrollRectToVisible:tableView.tableHeaderView.frameanimated:YES];

    }

    

    //返回的是section

    return index-1;

}