创建 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. Java面试题之类的静态代码块和静态属性等的加载顺序

    如以下面试题: /** * @author hjs * @date 2019/3/62:41 * @Dec */public class B { public static B t1 = new B( ...

  2. 转:Java SoftReference 使用构建对象缓存

    本文介绍对象的强.软.弱和虚引用的概念.应用及其在UML中的表示. 1.对象的强.软.弱和虚引用   在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.也就是说, ...

  3. linux下scp

    scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的.可能会稍微影响一下速度.当你服务器 ...

  4. weblogic12c配置免密码启动

    在运行startWeblogic.sh时需要输入有效的账号密码才能启动weblogic,为简化操作,可以配置boot.properties来免输账号密码,配置方法如下:1.查看在./domains/x ...

  5. 创建型设计模式之原型模式(Prototype)

    结构   意图 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 适用性 当要实例化的类是在运行时刻指定时,例如,通过动态装载:或者 为了避免创建一个与产品类层次平行的工厂类层次时:或 ...

  6. [ CodeVS冲杯之路 ] P3117

    不充钱,你怎么AC? 题目:http://codevs.cn/problem/3117/ 啊啊啊,基础的高精度乘法被我写得又臭又长,以后再来优化代码(DP着哪天能够把加减乘除全部写一边贴上来,哦对还有 ...

  7. 安装python 科学计算库

    http://www.softpedia.com/get/Programming/Other-Programming-Files/Python-x-y.shtml Pythonxy兴趣小组QQ群237 ...

  8. SVN如何进行版本的还原

    http://jingyan.baidu.com/article/d621e8da0d07022865913fa5.html 工具/原料 SVN乌龟软件和相关的文件 百度经验:jingyan.baid ...

  9. error LNK2019: 无法解析的外部符号 _deflate

    我的环境为: Win764 + VS2005 + zlib1.2.8 zlib1.2.8我使用VS2010来编译. ------------------------------------------ ...

  10. Appium+python自动化13-native和webview切换【转载】

    前言 现在大部分app都是混合式的native+webview,对应native上的元素通过uiautomatorviewer很容易定位到,webview上的元素就无法识别了. 一.识别webview ...