前几天实现iBooks类似的图书列表形式,share一下,效果如下。

实现关键代码原理:

1:创建UIt=TableView对象时,设置背景透明,分隔条透明

[plain] view plaincopyprint?
  1. // 设置table的分割符透明
  2. tbView.separatorColor = [UIColor clearColor];
  3. // 设置table背景透明
  4. tbView.backgroundColor = [UIColor clearColor];

// 设置table的分割符透明 tbView.separatorColor = [UIColor clearColor]; // 设置table背景透明 tbView.backgroundColor = [UIColor clearColor];

2:在tableView:cellForRowAtIndexPath中绘制cell内容,展示图书。这里一个技巧是自定义一个UIButton,覆盖在图书图片上,相应点击事件。其中使用button的tag来保存table数组中的所在图书的下标。

[plain] view plaincopyprint?
  1. // Customize the appearance of table view cells.
  2. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  3. static NSString *identifier = @"etuancell";
  4. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  5. if (!cell) {
  6. //cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:identifier];
  7. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
  8. [cell setAccessoryType:UITableViewCellAccessoryNone];
  9. // 取消选择模式
  10. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  11. }else{
  12. // 删除cell中的子对象,刷新覆盖问题。
  13. while ([cell.contentView.subviews lastObject] != nil) {
  14. [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
  15. }
  16. }
  17. // 定义图书大小
  18. #define kCell_Items_Width 156
  19. #define kCell_Items_Height 230
  20. // 设置图片大小206*306
  21. // 图片与图片之间距离为50
  22. // 每行3,4本图书
  23. CGFloat x = 80;
  24. CGFloat y = 40;
  25. NSInteger nowNum = kNum;
  26. if (bLandScape) {
  27. nowNum += 1;
  28. }
  29. NSInteger row = indexPath.row * nowNum;
  30. // 循环绘制出图书图片
  31. for (int i = 0; i<nowNum; ++i) {
  32. // 跳出循环
  33. if (row >= [data count]) {
  34. break;
  35. }
  36. // 展示图片
  37. UIImageView *bookView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, kCell_Items_Width, kCell_Items_Height)];
  38. NSString *bookName = [[NSString alloc] initWithFormat:@"book%d.png",row];
  39. bookView.image = [UIImage imageNamed:bookName];
  40. [cell.contentView addSubview:bookView];
  41. // 添加按钮
  42. UIButton *bookButton = [UIButton buttonWithType:UIButtonTypeCustom];
  43. bookButton.frame = CGRectMake(x, y, kCell_Items_Width, kCell_Items_Height);
  44. // 这里采用一个技巧,使用button的tag,记录tabledata中的序号
  45. bookButton.tag = row;
  46. [bookButton addTarget:self action:@selector(testButton:) forControlEvents:UIControlEventTouchUpInside];
  47. [cell.contentView addSubview:bookButton];
  48. x += (80+kCell_Items_Width);
  49. // row+1
  50. ++row;
  51. }
  52. return cell;
  53. }

// 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

[plain] view plaincopyprint?
  1. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  2. // Return the number of rows in the section.
  3. NSInteger count = ([data count]+kNum-1)/kNum;
  4. if (bLandScape) {
  5. count = ([data count]+kNum)/(kNum+1);
  6. }
  7. return count;
  8. }

- (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类似的图书列表形式相关推荐

  1. 自定义UITableViewCell实现类似iBooks图书列表形式

    http://www.cocoachina.com/bbs/read.php?tid=92144&fpage=2 转载于:https://www.cnblogs.com/ouyangfang/ ...

  2. 【15】Vue:02-Vue表单基本操作、表单修饰符、自定义指令、计算属性computed、侦听器watch、过滤器、生命周期、数组变异方法、替换数组、动态数组响应数据、图书列表案例、TODOS案例

    文章目录 day02 Vue常用特性 表单基本操作 表单修饰符 自定义指令 Vue.directive 注册全局指令 Vue.directive 注册全局指令 带参数 自定义指令局部指令 计算属性 c ...

  3. Android 自定义RecycleView实现多级树(类似qq分组列表)

    由于工作需要开发一个类似qq分组列表功能,所以就自定义RecycleView来实现功能 效果图: 自定义View: import java.util.ArrayList; import java.ut ...

  4. Abp vnext Web应用程序开发教程 2 —— 图书列表页面

    文章目录 关于本教程 下载源代码 动态JavaScript代理 在开发者控制台中进行测试 本地化 创建书籍页面 将书籍页面添加到主菜单 图书列表 运行最终应用程序 下一部分 关于本教程 本教程基于版本 ...

  5. AndroidStudio案例——图书列表

    目录 实验内容及步骤 步骤: 运行结果: 1.配置drawable文件 2.配置listiem.xml文件 3.导入到activity_main.xml中 4.配置Java代码 [Java代码详解] ...

  6. 案例:图书管理(包括图书列表展示,添加、修改、删除图书功能)

    案例:图书管理 功能如下: (1)图书列表 实现静态列表效果 基于数据实现模板效果 处理每行的操作按钮 (2)添加图书 实现表单的静态效果 添加图书表单域数据绑定 添加按钮事件绑定 实现添加业务逻辑 ...

  7. 自定义UITableViewCell需注意的问题

    自定义cell,有下面几种方法 方法一: 在controller的.m中 1 @interface ViewController ()<UITableViewDataSource,UITable ...

  8. iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局

    iOS开发UI篇-使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 iOS开发UI篇-使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目 ...

  9. 对自定义UITableViewCell的理解

    自定义UITableViewCell有两种方法: 1.较早版本 子类UITableViewCell   并利用xib构造 2.利用storyboard直接自定义cell 1.利用xib 设计好自定义的 ...

最新文章

  1. 函数重载和 函数模板
  2. hive的变量传递设置
  3. python3 print设置不换行
  4. P3605 [USACO17JAN]Promotion Counting P(树状数组)
  5. 查找工具locate和find
  6. 求职者被字节HR放鸽子?
  7. 用BULK INSERT命令导入数据详解
  8. python微控制器编程从零开始下载_Python微控制器编程从零开始(使用MicroPython)...
  9. 计算机内录,如何电脑内部录音,介绍一款可以录制电脑内部声音的工具
  10. Mex HDU - 4747(递推, 思维)
  11. 1月16日云栖精选夜读 | 阿里P8架构师谈:Zookeeper的原理和架构设计,以及应用场景... 1
  12. TLC5615 产生频率可变的正弦波
  13. Android - 手机下载的缓存视频在文件管理怎么找不到?
  14. 为了旅游和梁定郊大吵一次,此行贿赠喜爱的朋友!!!
  15. java毕业设计网上书城系统(附源码、数据库)
  16. Windows 快捷方式
  17. c语言编程实现简单的注册登录,C语言编程:实现用户的注册和登录
  18. Neo4j3-Neo4j基础操作(中)
  19. 【生活】外包到阿里工作是一种什么样到体验
  20. java期末考试复习题_JAVA期末考试复习试题

热门文章

  1. 计算机进化 放置游戏,随心放置冰风谷
  2. 一款超好用的截图软件Snipaste
  3. Open edX架构
  4. 遇到问题2:onLoad中使用setData无效
  5. matlab如何新建mat文件,如何在Matlab中创建.mat文件?
  6. spring注解驱动开发-4 Spring 自动装配
  7. 霍金:外星人存在但别主动去寻找(组图)
  8. 关于专利书写以及申报的一点心得体会
  9. 28:Maximum sum
  10. Leetcode 500题AC的刷题总结(C与C++)