天天看点

编辑表格@tableView实现分区,索引

//uitableviewdatasource代理中的方法,该方法返回值用于在表格右侧建立一列浮动的索引

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

{

    return stroies;

}

//
//  ViewController.m
//  tableview多分区和分区索引
//
//  Created by xxt-imac on 16/1/13.
//  Copyright © 2016年 xxt-imac. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSDictionary *tabelData;
    NSArray *stroies;
}
@property (strong, nonatomic) IBOutlet UITableView *table;

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    tabelData = [NSDictionary dictionaryWithObjectsAndKeys:
                 [NSArray arrayWithObjects:@"孙悟空", @"猪八戒",@"唐僧",@"沙和尚",@"小龙马",@"白骨精",nil],@"西游记",
                 [NSArray arrayWithObjects:@"贾宝玉",@"林岛屿",@"探春",@"洗出",@"可是", nil],@"红楼梦",
                 [NSArray arrayWithObjects:@"松江",@"灵宠",@"武动",@"武大郎",@"西门庆",@"养殖", nil],@"水浒传",
                 [NSArray arrayWithObjects:@"孙悟空", @"猪八戒",@"唐僧",@"沙和尚",@"小龙马",@"白骨精",nil],@"mama记",nil];
    stroies = [[tabelData allKeys]sortedArrayUsingSelector:@selector(compare:)];

    self.table.delegate = self;
    self.table.dataSource = self;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *stroy = [stroies objectAtIndex:section];
    return [[tabelData objectForKey:stroy] count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return stroies.count;
    
}
//uitableviewdatasource代理中的方法,该方法返回值用于在表格右侧建立一列浮动的索引
- ( NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return stroies;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger sectionNo = indexPath.section;
    NSInteger rowNo = indexPath.row;
    
    static NSString *cellID = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    NSString *stroy = [stroies objectAtIndex:sectionNo];
    cell.textLabel.text = [[tabelData objectForKey:stroy] objectAtIndex:rowNo];
    
    return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [stroies objectAtIndex:section];
}
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    
    NSString *str = [stroies objectAtIndex:section];
    
    return [NSString stringWithFormat:@"一共有%lu个人物",[[tabelData objectForKey:str] count]];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
           
效果图如下:<img src="https://img-blog.csdn.net/20160113163027840?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />