iOS中TableView的不同类型
2023-03-15 23:19:52 时间
TableView是iOS开发中经常用到的View,针对不同的显示需求,我们需要不同的Cell来进行显示,比较复杂的显示我们一般会自定义Cell的样式,但是简单的显示就可以靠iOS本身支持的列表类型了。
iOS目前支持四中列表类型,分别是:
- UITableViewCellStyleDefault:默认类型,可以显示图片和文本
- UITableViewCellStyleSubtitle:可以显示图片、文本和子文本
- UITableViewCellStyleValue1:可以显示图片、文本和子文本
- UITableViewCellStyleValue2:可以显示文本和子文本
其显示的样式也各不相同,按顺序如下所示:
要设置也很简单,代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
// 共四种类型
switch (indexPath.row) {
case 0:// UITableViewCellStyleDefault:默认的类型,支持显示图片和文本
{
NSString *CellOne = @"CellOne";
// 设置tableview类型
cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellOne];
// 设置不可点击
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.imageView.image = [UIImage imageNamed:@"icon"];// 图片
cell.textLabel.text = @"textLabel";// 文本
}
break;
case 1:// UITableViewCellStyleSubtitle类型,支持显示图片和文本以及子文本
{
NSString *CellTwo = @"CellTwo";
// 设置tableview类型
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellTwo];
// 设置不可点击
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.imageView.image = [UIImage imageNamed:@"icon"];// 图片
cell.textLabel.text = @"textLabel";// 文本
cell.detailTextLabel.text = @"detailTextLabel";// 子文本
}
break;
case 2:// UITableViewCellStyleValue1类型,支持显示图片和文本以及子文本
{
NSString *CellThree = @"CellThree";
// 设置tableview类型
cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellThree];
// 设置不可点击
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.imageView.image = [UIImage imageNamed:@"icon"];// 图片
cell.textLabel.text = @"textLabel";// 文本
cell.detailTextLabel.text = @"detailTextLabel";// 子文本
}
break;
case 3:// UITableViewCellStyleValue2类型,支持显示文本以及子文本
{
NSString *CellFour = @"CellFour";
// 设置tableview类型
cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellFour];
// 设置不可点击
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text = @"textLabel";// 文本
cell.detailTextLabel.text = @"detailTextLabel";// 子文本
}
break;
}
return cell;
}
可以在我的github获取示例工程:https://github.com/Cloudox/TableTypeDemo
相关文章
- 金融服务领域的大数据:即时分析
- 影响大数据、机器学习和人工智能未来发展的8个因素
- 从0开始构建一个属于你自己的PHP框架
- 如何将Hadoop集成到工作流程中?这6个优秀实践必看
- SEO公司使用大数据优化其模型的5种方法
- 关于Web Workers你需要了解的七件事
- 深入理解HTTPS原理、过程与实践
- 增强分析:数据和分析的未来
- PHP协程实现过程详解
- AI专家:大数据知识图谱——实战经验总结
- 关于PHP的错误机制总结
- 利用数据分析量化协同过滤算法的两大常见难题
- 怎么做大数据工作流调度系统?大厂架构师一语点破!
- 2019大数据处理必备的十大工具,从Linux到架构师必修
- OpenCV中的KMeans算法介绍与应用
- 教大家如果搭建一套phpstorm+wamp+xdebug调试PHP的环境
- CentOS下三种PHP拓展安装方法
- Go语言HTTP Server源码分析
- Go语言HTTP Server源码分析
- 2017年4月编程语言排行榜:Hack首次进入前五十