博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UI_UITableView_搭建
阅读量:6969 次
发布时间:2019-06-27

本文共 3637 字,大约阅读时间需要 12 分钟。

创建 tableView

UITableViewStyle 有两种选择

#pragma mark - 创建 tableView- (void)createTableView{    // 枚举类型共同拥有两个    self.mainTableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];    [self addSubview:self.mainTableView];}

根视图控制器遵守协议

@interface RootViewController () 
- (void)viewDidLoad {    [super viewDidLoad];    // 设置数据源代理    self.rootView.mainTableView.dataSource = self;    // 设置 delegate    self.rootView.mainTableView.delegate = self;    self.title = @"联系人";}

重写代理方法

返回分组的个数

#pragma -mark 返回分组的个数// 默觉得 1- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 2;}

每一个分组显示多少行数据 必须

#pragma mark - 每一个分组显示多少行数据 *必须*// dataSource 下方法- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return self.nameArray.count;}

每行显示什么内容 必须

#pragma mark - 每行显示什么内容 *必须*// dataSource 下方法- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    // 四种类型    // UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];    // static 作用    /**     *  1、清零功能 static int a;     *  2、保值作用     *  3、隐藏功能      */    // 静态变量 保值作用 仅仅创建一次    static NSString *cell_id = @"UITableViewCell";    // 利用重用创建    UITableViewCell *cell = nil;    // 在重用池查找    cell = [tableView dequeueReusableCellWithIdentifier:cell_id];    if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cell_id];    }    cell.textLabel.text = self.nameArray[indexPath.row];//    indexPath.section  那个分组//    indexPath.row    哪一行    NSString *name = [NSString stringWithFormat:@"%ld.png", indexPath.row + 1];    cell.imageView.image = [UIImage imageNamed:name];    // 显示具体信息 cell 类型用 UITableViewCellStyleSubtitle    cell.detailTextLabel.text = self.numberArray[indexPath.row];    // 右側附件button 枚举类型    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;    return cell;}

row 的高度

#pragma mark - 跳转 row 的高度- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 80.0f;}

返回分组的名字

#pragma mark - 返回分组的名字- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    if (section == 0) {        return @"分组一";    }    return @"分组二";}

设置分组头部

#pragma mark - 设置分组头部- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 50)];    label.textAlignment = NSTextAlignmentCenter;    label.textColor = [UIColor redColor];    if (section == 0) {        label.text = @"1组";    } else {        label.text = @"2组";    }    return label;}

设置分组头部的高度

#pragma mark - 设置分组头部的高度- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{    return 60.0f;}#pragma mark - 设置分组头部的高度- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{    return 60.0f;}

cell 的点击事件

#pragma mark - cell 的点击事件- (void)tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    // 取消选中状态    [tableView deselectRowAtIndexPath:indexPath animated:YES];    SecondViewController *secondVC = [[SecondViewController alloc] init];    [self.navigationController pushViewController:secondVC animated:YES];}

实现索引

#pragma mark - 实现索引- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{    return @[@"1组", @"2组"];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}

转载地址:http://dussl.baihongyu.com/

你可能感兴趣的文章
Windows Server入门系列之十二 进程管理
查看>>
搭建DHCP服务器
查看>>
Linux的watch命令 — 实时监测命令的运行结果
查看>>
IO多路复用之epoll总结
查看>>
关于计算机脱域
查看>>
cisco c4507R-E 交换机启动不起来
查看>>
java web 自定义标签
查看>>
2019活动技术(Eventech)的新趋势
查看>>
mac系统安装/升级node
查看>>
数据库-----同义词
查看>>
python venv actieve uninstall pack-name sitepage
查看>>
10月份技术:PXE批量安装LINUX系统
查看>>
linux head
查看>>
热插拔——矿机先行利器
查看>>
rsync 远程同步 实时同步备份 两种免交互的方式实现实时备份
查看>>
什么是灰度发布,以及灰度发布A/B测试
查看>>
Spring全家桶系列--SpringBoot与Mybatis结合
查看>>
使用Java SDK实现离线签名
查看>>
VRRP工作原理
查看>>
2019年的财富管理行业将何去何从?
查看>>