UITableView实现通讯录效果

//
//  TableViewIndexViewController.m
//  study2024
//
//  Created by figo  zhu on 2024/12/22.
//

#import "TableViewIndexViewController.h"
//实现协议UITableViewDelegate,UITableViewDataSource
@interface TableViewIndexViewController ()<UITableViewDelegate,UITableViewDataSource>
//通讯录索引tableview
@property (weak, nonatomic) IBOutlet UITableView *indexTableView;
//通讯录组
@property (strong,nonatomic) NSArray *sectionGroup;
//通讯录所有名字
@property (strong,nonatomic) NSArray<NSArray *> *sectionNames;

@end

@implementation TableViewIndexViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //数据源设置为自己
    self.indexTableView.dataSource=self;
    //代理设置为自己
    self.indexTableView.delegate=self;
    //获取屏幕大小
    int screenWidth = UIScreen.mainScreen.bounds.size.width;
    int screenHeight = UIScreen.mainScreen.bounds.size.height;
    int x = UIScreen.mainScreen.bounds.origin.x;
    int y = UIScreen.mainScreen.bounds.origin.x;
    //设置tableview的左上角坐标,及宽和高
    [self.indexTableView setFrame:CGRectMake(x, y, screenWidth, screenHeight)];

    //通讯录组
    self.sectionGroup=[[NSArray alloc] initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",nil];
    //通讯录名称
    self.sectionNames=[[NSArray alloc] initWithObjects:@[@"阿猫",@"阿狗"], @[@"步青云",@"步步高"],@[@"曹操",@"陈陈"],@[@"当当",@"弟弟"],@[@"艺龙",@"依依"],@[@"芳芳",@"福临"],@[@"高一",@"高二",@"高三"],@[@"黄帝",@"黄蓉"],nil];
    //设置索引颜色
    self.indexTableView.sectionIndexColor=[UIColor blueColor];
    //设置索引背景颜色
    self.indexTableView.sectionIndexBackgroundColor=[UIColor greenColor];

    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
//每行的数据展示
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    UITableViewCell *cell=[self.indexTableView dequeueReusableCellWithIdentifier:@"abc"];
    if(cell==nil){
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"abc"];
    }
    cell.textLabel.text=self.sectionNames[indexPath.section][indexPath.row];
    return cell;
}
//每一段的行数
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.sectionNames[section].count;
}
//总共有几段
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.sectionGroup.count;
}
//每一段的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.sectionGroup[section];
}
//返回分组标题数组
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.sectionGroup;
}
//获取点击的索引标题
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    NSLog(@"你点击的索引是%@",title);
    return index;
}
@end

效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值