自定义UITableViewCell实现ibooks类似的图书列表形式
前几天实现iBooks类似的图书列表形式,share一下,效果如下。
实现关键代码原理:
1:创建UIt=TableView对象时,设置背景透明,分隔条透明
- // 设置table的分割符透明
- tbView.separatorColor = [UIColor clearColor];
- // 设置table背景透明
- tbView.backgroundColor = [UIColor clearColor];
// 设置table的分割符透明 tbView.separatorColor = [UIColor clearColor]; // 设置table背景透明 tbView.backgroundColor = [UIColor clearColor];
2:在tableView:cellForRowAtIndexPath中绘制cell内容,展示图书。这里一个技巧是自定义一个UIButton,覆盖在图书图片上,相应点击事件。其中使用button的tag来保存table数组中的所在图书的下标。
- // Customize the appearance of table view cells.
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *identifier = @"etuancell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
- if (!cell) {
- //cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:identifier];
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
- [cell setAccessoryType:UITableViewCellAccessoryNone];
- // 取消选择模式
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- }else{
- // 删除cell中的子对象,刷新覆盖问题。
- while ([cell.contentView.subviews lastObject] != nil) {
- [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
- }
- }
- // 定义图书大小
- #define kCell_Items_Width 156
- #define kCell_Items_Height 230
- // 设置图片大小206*306
- // 图片与图片之间距离为50
- // 每行3,4本图书
- CGFloat x = 80;
- CGFloat y = 40;
- NSInteger nowNum = kNum;
- if (bLandScape) {
- nowNum += 1;
- }
- NSInteger row = indexPath.row * nowNum;
- // 循环绘制出图书图片
- for (int i = 0; i<nowNum; ++i) {
- // 跳出循环
- if (row >= [data count]) {
- break;
- }
- // 展示图片
- UIImageView *bookView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, kCell_Items_Width, kCell_Items_Height)];
- NSString *bookName = [[NSString alloc] initWithFormat:@"book%d.png",row];
- bookView.image = [UIImage imageNamed:bookName];
- [cell.contentView addSubview:bookView];
- // 添加按钮
- UIButton *bookButton = [UIButton buttonWithType:UIButtonTypeCustom];
- bookButton.frame = CGRectMake(x, y, kCell_Items_Width, kCell_Items_Height);
- // 这里采用一个技巧,使用button的tag,记录tabledata中的序号
- bookButton.tag = row;
- [bookButton addTarget:self action:@selector(testButton:) forControlEvents:UIControlEventTouchUpInside];
- [cell.contentView addSubview:bookButton];
- x += (80+kCell_Items_Width);
- // row+1
- ++row;
- }
- return cell;
- }
// Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *identifier = @"etuancell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];if (!cell) {//cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:identifier];cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];[cell setAccessoryType:UITableViewCellAccessoryNone];// 取消选择模式cell.selectionStyle = UITableViewCellSelectionStyleNone;}else{// 删除cell中的子对象,刷新覆盖问题。while ([cell.contentView.subviews lastObject] != nil) {[(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];}}// 定义图书大小 #define kCell_Items_Width 156 #define kCell_Items_Height 230// 设置图片大小206*306// 图片与图片之间距离为50// 每行3,4本图书CGFloat x = 80;CGFloat y = 40;NSInteger nowNum = kNum;if (bLandScape) {nowNum += 1;}NSInteger row = indexPath.row * nowNum;// 循环绘制出图书图片 for (int i = 0; i<nowNum; ++i) {// 跳出循环if (row >= [data count]) {break;}// 展示图片UIImageView *bookView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, kCell_Items_Width, kCell_Items_Height)];NSString *bookName = [[NSString alloc] initWithFormat:@"book%d.png",row];bookView.image = [UIImage imageNamed:bookName];[cell.contentView addSubview:bookView];// 添加按钮UIButton *bookButton = [UIButton buttonWithType:UIButtonTypeCustom];bookButton.frame = CGRectMake(x, y, kCell_Items_Width, kCell_Items_Height);// 这里采用一个技巧,使用button的tag,记录tabledata中的序号bookButton.tag = row;[bookButton addTarget:self action:@selector(testButton:) forControlEvents:UIControlEventTouchUpInside];[cell.contentView addSubview:bookButton];x += (80+kCell_Items_Width);// row+1++row;}return cell; }
3:在tableView:numberOfRowInSection中,动态返回tableview的row数量,其中kNum为3
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- // Return the number of rows in the section.
- NSInteger count = ([data count]+kNum-1)/kNum;
- if (bLandScape) {
- count = ([data count]+kNum)/(kNum+1);
- }
- return count;
- }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {// Return the number of rows in the section.NSInteger count = ([data count]+kNum-1)/kNum;if (bLandScape) {count = ([data count]+kNum)/(kNum+1);}return count; }
4:更多效果参考
源代码下载,点击这里 。如果您有任何问题或者疑问可以随时联系我。转发请注明http://blog.csdn.net/ugg
http://blog.csdn.net/ugg/article/details/7237811
自定义UITableViewCell实现ibooks类似的图书列表形式相关推荐
- 自定义UITableViewCell实现类似iBooks图书列表形式
http://www.cocoachina.com/bbs/read.php?tid=92144&fpage=2 转载于:https://www.cnblogs.com/ouyangfang/ ...
- 【15】Vue:02-Vue表单基本操作、表单修饰符、自定义指令、计算属性computed、侦听器watch、过滤器、生命周期、数组变异方法、替换数组、动态数组响应数据、图书列表案例、TODOS案例
文章目录 day02 Vue常用特性 表单基本操作 表单修饰符 自定义指令 Vue.directive 注册全局指令 Vue.directive 注册全局指令 带参数 自定义指令局部指令 计算属性 c ...
- Android 自定义RecycleView实现多级树(类似qq分组列表)
由于工作需要开发一个类似qq分组列表功能,所以就自定义RecycleView来实现功能 效果图: 自定义View: import java.util.ArrayList; import java.ut ...
- Abp vnext Web应用程序开发教程 2 —— 图书列表页面
文章目录 关于本教程 下载源代码 动态JavaScript代理 在开发者控制台中进行测试 本地化 创建书籍页面 将书籍页面添加到主菜单 图书列表 运行最终应用程序 下一部分 关于本教程 本教程基于版本 ...
- AndroidStudio案例——图书列表
目录 实验内容及步骤 步骤: 运行结果: 1.配置drawable文件 2.配置listiem.xml文件 3.导入到activity_main.xml中 4.配置Java代码 [Java代码详解] ...
- 案例:图书管理(包括图书列表展示,添加、修改、删除图书功能)
案例:图书管理 功能如下: (1)图书列表 实现静态列表效果 基于数据实现模板效果 处理每行的操作按钮 (2)添加图书 实现表单的静态效果 添加图书表单域数据绑定 添加按钮事件绑定 实现添加业务逻辑 ...
- 自定义UITableViewCell需注意的问题
自定义cell,有下面几种方法 方法一: 在controller的.m中 1 @interface ViewController ()<UITableViewDataSource,UITable ...
- iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
iOS开发UI篇-使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 iOS开发UI篇-使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目 ...
- 对自定义UITableViewCell的理解
自定义UITableViewCell有两种方法: 1.较早版本 子类UITableViewCell 并利用xib构造 2.利用storyboard直接自定义cell 1.利用xib 设计好自定义的 ...
最新文章
- 函数重载和 函数模板
- hive的变量传递设置
- python3 print设置不换行
- P3605 [USACO17JAN]Promotion Counting P(树状数组)
- 查找工具locate和find
- 求职者被字节HR放鸽子?
- 用BULK INSERT命令导入数据详解
- python微控制器编程从零开始下载_Python微控制器编程从零开始(使用MicroPython)...
- 计算机内录,如何电脑内部录音,介绍一款可以录制电脑内部声音的工具
- Mex HDU - 4747(递推, 思维)
- 1月16日云栖精选夜读 | 阿里P8架构师谈:Zookeeper的原理和架构设计,以及应用场景... 1
- TLC5615 产生频率可变的正弦波
- Android - 手机下载的缓存视频在文件管理怎么找不到?
- 为了旅游和梁定郊大吵一次,此行贿赠喜爱的朋友!!!
- java毕业设计网上书城系统(附源码、数据库)
- Windows 快捷方式
- c语言编程实现简单的注册登录,C语言编程:实现用户的注册和登录
- Neo4j3-Neo4j基础操作(中)
- 【生活】外包到阿里工作是一种什么样到体验
- java期末考试复习题_JAVA期末考试复习试题