自定义UITableViewCell实现ibooks类似的图书列表形式
来源:互联网 发布:java 方法里套方法 编辑:程序博客网 时间:2024/05/17 02:40
前几天实现iBooks类似的图书列表形式,share一下,效果如下。
实现关键代码原理:
1:创建UIt=TableView对象时,设置背景透明,分隔条透明
- // 设置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;
- }
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;
- }
4:更多效果参考
源代码下载,点击这里 。如果您有任何问题或者疑问可以随时联系我。转发请注明http://blog.csdn.net/ugg
http://blog.csdn.net/ugg/article/details/7237811
- 自定义UITableViewCell实现ibooks类似的图书列表形式
- 自定义UITableViewCell实现ibooks类似的图书列表形式
- 自定义UITableViewCell,实现类似QQ好友列表
- 定制UITableviewCell实现类似微博的分组效果
- 纯代码实现UITableViewCell的自定义扩展
- 实现一个公用的自定义UITableviewCell
- QListWidget实现图片缩略图形式的列表
- 类似列表滑动块的实现
- 自定义UITableViewCell的内容
- 自定义UITableViewCell的内容
- 自定义UITableViewCell的背景
- 自定义的UITableViewCell
- 自定义 UITableViewCell 的布局
- 自定义UITableViewCell的样式
- UITableViewCell的自定义问题
- UITableViewCell的自定义
- UITableViewcell 样式的自定义
- C# 类似qq聊天的群列表成员自定义控件
- 常用正则表达式
- 抽象类和接口
- hibernate学习笔记
- JavaScript_语法
- [共享] 达芬奇DM6468算法移植手册--Linux下XDM标准算法DSP端处理步骤
- 自定义UITableViewCell实现ibooks类似的图书列表形式
- 换行符‘\n’和回车符‘\r’
- Excel 使用技巧集锦——163种技巧
- UITableViewCell的重绘机制导致的列状态问题
- Java面试
- CTE(Common Table Expressions)简单介绍(四)
- 确保对象的唯一性——单例模式 (一)
- 确保对象的唯一性——单例模式 (二)
- 确保对象的唯一性——单例模式 (三)