通信録インデックスの符号化を一歩一歩教えてあげます
4211 ワード
#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