创建 tableView

UITableViewStyle 有两种选择

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

根视图控制器遵守协议

@interface RootViewController () <UITableViewDataSource, UITableViewDelegate>
- (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.
}

UI_UITableView_搭建的更多相关文章

  1. Online Judge(OJ)搭建(第一版)

    搭建 OJ 需要的知识(重要性排序): Java SE(Basic Knowledge, String, FileWriter, JavaCompiler, URLClassLoader, Secur ...

  2. Angular2入门系列教程1-使用Angular-cli搭建Angular2开发环境

    一直在学Angular2,百忙之中抽点时间来写个简单的教程. 2016年是前端飞速发展的一年,前端越来越形成了(web component)组件化的编程模式:以前Jquery通吃一切的田园时代一去不复 ...

  3. 总结:Mac前端开发环境的搭建(配置)

    新年新气象,在2016年的第一天,我入手了人生中第一台自己的电脑(大一时好友赠送的电脑在一次无意中烧坏了主板,此后便不断借用别人的或者网站的).macbook air,身上已无分文...接下来半年的房 ...

  4. Angular企业级开发(5)-项目框架搭建

    1.AngularJS Seed项目目录结构 AngularJS官方网站提供了一个angular-phonecat项目,另外一个就是Angular-Seed项目.所以大多数团队会基于Angular-S ...

  5. 【分享】标准springMVC+mybatis项目maven搭建最精简教程

    文章由来:公司有个实习同学需要做毕业设计,不会搭建环境,我就代劳了,顺便分享给刚入门的小伙伴,我是自学的JAVA,所以我懂的.... (大图直接观看显示很模糊,请在图片上点击右键然后在新窗口打开看) ...

  6. 一起学微软Power BI系列-使用技巧(4)Power BI中国版企业环境搭建和帐号问题

    千呼万唤的Power BI中国版终于落地了,相信12月初的微软技术大会之后已经铺天盖地的新闻出现了,不错,Power BI中国版真的来了,但还有些遗憾,国际版的一些重量级服务如power bi emb ...

  7. 从0开始搭建SQL Server AlwaysOn 第一篇(配置域控)

    从0开始搭建SQL Server AlwaysOn 第一篇(配置域控) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www.cnb ...

  8. 从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)

    从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www ...

  9. 从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn)

    从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://w ...

随机推荐

  1. 为什么说for循环设置循环变量的那部分是一个父作用域?

    最近在看阮一峰老师的<ES6> 看到let时,发现一处for循环很神奇的地方. 书中的原话是:“另外,for循环还有一个特别之处,就是设置变量的那部分是一个父作用域,而循环体内部是一个单独 ...

  2. WKWebview 和 WebViewJavascriptBridge

    WKWebview 和 WebViewJavascriptBridge https://www.cnblogs.com/L-vincen/p/6681435.html 链接在这里,有很多不错的文章,大 ...

  3. Linux 安装配置JDK

    一.下载jdk 参考:http://www.codingyun.com/article/40.html 可以先下载到本地,然后ftp到服务器 也可以直接在服务器下载(windows版本的区分32位与6 ...

  4. Broadcom GNSS xxx Geolocaltion Sensor与Windows导航程序的兼容性(转)

    Broadcom是Windows 8(3G)平板普遍采用的一款GPS传感器, 其windows驱动程序可以提供GNSS接口.GNSS接口提供的数据,说实话确实比普通手机的数据好.在开机.室外.无AGP ...

  5. matlab多行注释的三种方法

    转自:http://icictech.blog.163.com/blog/static/2797425420084595725499/ 1)方法一 选中你要加注释的内容,然后选择工具菜单"t ...

  6. apache快速配置简易网站

    网站源文件目录 F:\01.prj\site\static_html F:\01.prj\site\static_html 打开apache_http.conf 1 .修改网站目录 错误1 Alias ...

  7. 【dlib代码解读】人脸检测器的训练【转】

    转自:http://blog.csdn.net/elaine_bao/article/details/53046542 版权声明:本文为博主原创文章,转载请注明.   目录(?)[-] 综述 代码解读 ...

  8. Appium+python自动化10-AVD 模拟器【转载】

    前言 有些小伙伴没android手机,这时候可以在电脑上开个模拟器玩玩 一.模拟器配置 1.双击启动AVD Manager,进入配置界面

  9. Linux操作常识

    1.分区 linux如果手动选择分区,必须的两个分区是根分区和swap分区,swap分区是与内存的交换分区,通常设置大小为内存的两倍(如果内存够大也可以不用设置) 2.关机重启      命令:shu ...

  10. DRF的版本和认证

    DRF的版本 版本控制是做什么用的, 我们为什么要用 首先我们要知道我们的版本是干嘛用的呢~~大家都知道我们开发项目是有多个版本的~~ 当我们项目越来越更新~版本就越来越多~~我们不可能新的版本出了~ ...