一步一步教你完成通讯录索引的编码


#import 
@interface YUViewController : UIViewController

@end

#import "YUViewController.h"
#import "YUContactsTableViewCell.h"
#define kIdentifier @"YUContactsTableViewCell"

@interface YUViewController () 
{
    IBOutlet UITableView    *_myTableView;
    IBOutlet UILabel        *_myLabel;      //放大显示被选中的索引号
    NSMutableArray          *_myArrM;       //索引数组
}


@end


@implementation YUViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    [self initialization];
}


#pragma mark 初始化
-(void)initialization{
    
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.tableFooterView = [[UIView alloc] init];
    _myTableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [_myTableView registerNib:[UINib nibWithNibName:NSStringFromClass([YUContactsTableViewCell class]) bundle:nil] forCellReuseIdentifier:kIdentifier];
    
    _myLabel.layer.cornerRadius = _myLabel.bounds.size.width * 0.5;
    _myLabel.layer.masksToBounds = YES;
    _myLabel.alpha = 0;
}

-(void)initData{
    NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < 26; i++) {
        NSString *c = [NSString stringWithFormat:@"%c",i+65];
        [arrM addObject:c];
    }
    _myArrM = arrM;
}


#pragma mark UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
   return _myArrM.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    YUContactsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kIdentifier forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.context.text = [NSString stringWithFormat:@"第%ld组第%ld行",indexPath.section,indexPath.row];
    return cell;
}


//返回索引列表所需的内容数组
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return _myArrM;
}


//当点击索引的第 index 编号时,告知UITableview相应的滚动到第几组,可以这里做一些额外的工作,比如这里增加了一个UILabel,用来做动画放大显示索引号。
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{

    _myLabel.text = _myArrM[index];
    [UIView animateWithDuration:1.0 animations:^{
        _myLabel.alpha = 1;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1 animations:^{
            _myLabel.alpha = 0;
        }];
    }];
    return index;
}

#pragma mark UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 29;
}


//自定义组头
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 29)];
    l.backgroundColor = [UIColor blueColor];
    l.text = [NSString stringWithFormat:@"%@",_myArrM[section]];
    l.textColor = [UIColor whiteColor];
    l.font = [UIFont boldSystemFontOfSize:15.0];
    l.textAlignment = NSTextAlignmentCenter;
    return l;
}



#import 

@interface YUContactsTableViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *context;
@end

#import "YUContactsTableViewCell.h"

@implementation YUContactsTableViewCell
{

    IBOutlet UIImageView *_icon;

}
- (void)awakeFromNib {
    [self initialization];
}

-(void)initialization{
    
    _icon.layer.cornerRadius = _icon.bounds.size.width * 0.5;
    _icon.layer.masksToBounds = YES;
    
    int number = arc4random() % 3;
    _icon.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",number]];
    
}

-(void)setContext:(UILabel *)context{
    _context = context;
    _context.textAlignment = NSTextAlignmentLeft;
}

@end



你可能感兴趣的:(UI,UITableview,索引,iOS开发)