IOS开发复习笔记(4)-TableView
2024-08-28 17:26:23
总结几个TableView常用的代码
1.初始化方面
static string CellIndetifier=@"cellIndetifier";
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;//表中段数
}
-(NSInterger)tableView:(UITableVIew *)tableView numberOfRowsInSection:(NSInteger)section{
retrun 0;//每个段中的单元格数量
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
cell.textLabel.text=@”showNSString”;//表中文字内容
UIImage *image=[UIImage imageNamed:@”picname.png”];
cell.imageView.image=image;//添加图片
cell.textLabel.font=[UIFont boldSystemFontOfSize:10];//设置字体大小
// Configure the cell.
return cell;
} //配置单元格
通过委托为每行的列表设置高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// return 70;//简单的操作
//return indexPath.row//为每行设置不同高度
}
在TableViewCell中添加子视图如Lable,继承UITableViewCell类
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
2.tableviewcell的样式有很那些呢:
UITableViewCellDefault样式:左边一个显示图片的imageView,一个标题textLabel,没有detailTextLabel。
UITableViewCellStyleSubtitle:左边一个显示图片的imageView,上边一个主标题textLabel,一个副标题detailTextLabel。主标题字体大且加黑,副标题字体小在主标题下边。
UITableViewCellStyleValue1:左边一个显示图片的imageView,左边一个主标题textLabel,右边一个副标题detailTextLabel,主标题字体比较黑。
UITableViewCellStyleValue2:左边一个主标题textLabel字体偏小,挨着右边一个副标题detailTextLabel,字体大且加黑。
通过委托设置缩进的级别,关键词indentation 为缩进,缩排的意思
-(NSInteger) tableView:(UITableView *)tableView indentationLevelForAtIndexPath:(NSIndexPath *)indexPath
{
return num;//num是你要缩进的级别
}
3.tableViewCell的选择处理
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
return indexPath;//返回选择的行数,由于是will所以是发生在选择行之前,可以增加一些如高亮操作,或者无法选中的功能
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//在选中该行后进行的操作,可以使用indexPath.row作为索引进行取数
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
//取消该行的操作,比如选了其他行,注意didselect和didDeselect区别,初学者可能经常没注意到
}
4.使用自定义的Cell类
UITableView *tableView=(id)[self.view viewWithTag:1];//话说这是另一种连接的方法?
[tableView regsiterClass:[BIDNameCell class] forCellReuseIndentifier:@"cellresusename"]
//BIDNameCell为自定义的Cell类,tableView是你声明的表视图
5.
最新文章
- C#图片存入数据库及其读出显示
- 精通 Angular JS 第一天——Angular 之禅
- Ubuntu 12.04(32位)安装Oracle 11g(32位)
- .NET 实现自定义ContextUser的Identity和Principal实现自定义用户信息,权限验证。
- Kafka原理与java simple producer示例
- Java学习-036-JavaWeb_005 -- JSP 动作标识 - forward
- 转:如何学习SQL(第三部分:SQL数据类型与三值逻辑)
- Android pix转换为sp
- Android源代码之Gallery专题研究(1)
- 07ADO.Net
- 一次awk脚本的重构
- mac 版本号控制工具SmartSVN7.5.4(破解版)
- 玩下Javascript
- 反射 与 preprrty装饰器
- 动态规划之139 Word Break
- 多媒体指令(AVX加速数组求和)
- C#使用System.IO.Path获取文件路径、文件名
- HDU 4734 - F(x) - [数位DP][memset优化]
- 频繁请求报requests异常的处理
- 【python】 time模块和datetime模块详解 【转】
热门文章
- Android - 错误:Unable to instantiate application
- Navicat Premium常用快捷键
- NodeJS 安装不存在的模块
- Mac 上的传奇效率神器 Alfred 3
- linux命令ulimit 系统限制你打开一些资源数
- gitlab 6 安装备忘录
- Atitit。Cas机制 软件开发 编程语言 无锁机制 java c# php
- jquery的基本api
- python简单处理xml文件
- Editing a Book UVA - 11212 IDA*