转自:http://www.cnblogs.com/lovecode/archive/2012/01/07/2315630.html

UITableView的强大更多程度上来自于可以任意自定义UITableViewCell单元格。通常,UITableView中的Cell是动态 的,在使用过程中,会创建一个Cell池,根据每个cell的高度(即tableView:heightForRowAtIndexPath:返回值), 以及屏幕高度计算屏幕中可显示几个cell。而进行自定义TableViewCell无非是采用代码实现或采用IB编辑nib文件来实现两种方式,本文主 要收集代码的方式实现各种cell自定义。

如何动态调整Cell高度

 1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 2
 3     static NSString *CellIdentifier = @"Cell";
 4
 5     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
 6     if (cell == nil) {
 7         cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
 8         UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
 9         label.tag = 1;
10         label.lineBreakMode = UILineBreakModeWordWrap;
11         label.highlightedTextColor = [UIColor whiteColor];
12         label.numberOfLines = 0;
13         label.opaque = NO; // 选中Opaque表示视图后面的任何内容都不应该绘制
14         label.backgroundColor = [UIColor clearColor];
15         [cell.contentView addSubview:label];
16         [label release];
17     }
18
19     UILabel *label = (UILabel *)[cell viewWithTag:1];
20     NSString *text;
21     text = [textArray objectAtIndex:indexPath.row];
22     CGRect cellFrame = [cell frame];
23     cellFrame.origin = CGPointMake(0, 0);
24
25     label.text = text;
26     CGRect rect = CGRectInset(cellFrame, 2, 2);
27     label.frame = rect;
28     [label sizeToFit];
29     if (label.frame.size.height > 46) {
30         cellFrame.size.height = 50 + label.frame.size.height - 46;
31     }
32     else {
33         cellFrame.size.height = 50;
34     }
35     [cell setFrame:cellFrame];
36
37     return cell;
38 }

1 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
2 {
3     UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
4     return cell.frame.size.height;
5 }

如何用图片自定义Table Separeator分割线
一般地,利用类似[tableView setSeparatorColor:[UIColor redColor]];语句即可修改cell中间分割线的颜色。那又如何用一个图片作为分割线背景呢?可以尝试如下:
方法一:
先设置cell separatorColor为clear,然后把图片做的分割线添加到自定义的custom cell上。

方法二:
在cell里添加一个像素的imageView后将图片载入进,之后设置tableView.separatorStyle = UITableViewCellSeparatorStyleNone

自定义首行Cell与其上面导航栏间距

1 tableView.tableHeaderView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,5,20)] autorelease];

自定义UITableViewCell的accessory样式
      默认的accessoryType属性有四种取值:UITableViewCellAccessoryNone、 UITableViewCellAccessoryDisclosureIndicator、 UITableViewCellAccessoryDetailDisclosureButton、 UITableViewCellAccessoryCheckmark。如果想使用自定义附件按钮的其他样式,则需使用UITableView的accessoryView属性来指定。

 1 UIButton *button;
 2 if(isEditableOrNot) {
 3     UIImage *image = [UIImage imageNamed:@"delete.png"];
 4     button = [UIButton buttonWithType:UIButtonTypeCustom];
 5     CGRect frame = CGRectMake(0.0,0.0,image.size.width,image.size.height);
 6     button.frame = frame;
 7     [button setBackgroundImage:image forState:UIControlStateNormal];
 8     button.backgroundColor = [UIColor clearColor];
 9     cell.accessoryView = button;
10 }else{
11     button = [UIButton buttonWithType:UIButtonTypeCustom];
12     button.backgroundColor = [UIColor clearColor];
13     cell.accessoryView = button;
14 }

以上代码仅仅是定义了附件按钮两种状态下的样式,问题是现在这个自定义附件按钮的事件仍不可用。即事件还无法传递到 UITableViewDelegate的accessoryButtonTappedForRowWithIndexPath方法上。当我们在上述代码 中在加入以下语句:
             [button addTarget:self action:@selector(btnClicked:event:) forControlEvents:UIControlEventTouchUpInside];
后, 虽然可以捕捉到每个附件按钮的点击事件,但我们还无法进行区别到底是哪一行的附件按钮发生了点击动作!因为addTarget:方法最多允许传递两个参 数:target和event,这两个参数都有各自的用途了(target指向事件委托对象,event指向所发生的事件)。看来只依靠Cocoa框架已 经无法做到了。

但我们还是可以利用event参数,在自定义的btnClicked方法中判断出事件发生在UITableView的哪一个cell上。因为UITableView有一个很关键的方法indexPathForRowAtPoint,可以根据触摸发生的位置,返回触摸发生在哪一个cell的indexPath。而且通过event对象,正好也可以获得每个触摸在视图中的位置。

 1 // 检查用户点击按钮时的位置,并转发事件到对应的accessory tapped事件
 2 - (void)btnClicked:(id)sender event:(id)event
 3 {
 4      NSSet *touches = [event allTouches];
 5      UITouch *touch = [touches anyObject];
 6      CGPoint currentTouchPosition = [touch locationInView:self.tableView];
 7      NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
 8      if(indexPath != nil)
 9      {
10          [self tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
11      }
12 }
13

这样,UITableView的accessoryButtonTappedForRowWithIndexPath方法会被触发,并且获得一个indexPath参数。通过这个indexPath参数,我们即可区分到底哪一行的附件按钮发生了触摸事件。

1 - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
2 {
3     int  *idx = indexPath.row;
4    //这里加入自己的逻辑
5 }

可任意自定义的UITableViewCell(转)相关推荐

  1. IOS自定义表格UITableViewCell

    在UITableView中,自定义表格,最原始是继承UITableViewCell,然后通过写代码方式去搞,但是这个费事了. 1.在storyboard中 给一个ViewController的tabi ...

  2. 【手把手教程】如何快速实现任意自定义的域名的网页跳转

    文章目录 1 前言 2 手把手步骤 3 更多分享 1 前言 之前我写过一篇博文介绍 如何配置任何自定义域名的网页跳转,里面讲到了背后的核心原理,及一些实操的步骤,可是最近有童鞋跟我反应,内容实在太长了 ...

  3. 苹果CMS、海洋CMS自动定时采集-可采集任意自定义指定资源

    使用苹果.海洋cms的是不是感觉资源更新很麻烦,虽然cms本身有定时采集,由于是php开发的,需要有访问才能触发,虽然有一些服务器控制面板可以新建定时任务,到指定时间去请求异常定时采集链接,但是这也是 ...

  4. excel 单元格与任意自定义内容拼接公式

    例子:1,'liux117','刘星',new date(),'liux117','刘星',new date(),1,1,1,'IV20201028000003','liux117',发票类型,'91 ...

  5. 基于Web的Kafka管理器工具之Kafka-manager的编译部署详细安装 (支持kafka0.8、0.9和0.10以后版本)(图文详解)(默认端口或任意自定义端口)...

    不多说,直接上干货! 至于为什么,要写这篇博客以及安装Kafka-manager? 问题详情 无奈于,在kafka里没有一个较好自带的web ui.启动后无法观看,并且不友好.所以,需安装一个第三方的 ...

  6. 博客园电子期刊2012年1月刊发布啦

    期刊访问网址:http://emag.cnblogs.com/2012/CNBlogsEmag201201.html.以下为本期期刊内容: 博客园电子期刊 No49.2012年1月刊 2012年1月编 ...

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

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

  8. CheckBox UITableViewCell

    一个简单的自定义CheckBox UITableViewCell 效果: UITableViewCheckBoxCell.h 1 // 2 // UITableViewCheckBoxCell.h 3 ...

  9. UITableViewCell中设置动态大小的圆形imageview

    2019独角兽企业重金招聘Python工程师标准>>> 在一个自定义的UITableViewCell里有一个imageview需要显示成圆形,因为它的宽高是autolayout动态设 ...

最新文章

  1. 必看,经典sql面试题(学生表_课程表_成绩表_教师表)
  2. 多才多艺的console
  3. OpenWrt——配置L2TP客户端
  4. RESET MASTER和RESET SLAVE使用场景和说明【转】
  5. 小米10青春版跑分流出:搭载骁龙720G芯片 下周一见!
  6. godaddy mysql 连接 设置 2014_GoDaddy主机数据库远程访问设置的方法
  7. dw实时视图与网页不一样_美团点评 Flink 实时数仓应用经验分享
  8. 类与对象(面向对象的编程语言java)
  9. win10无法修改mac地址_Oops,手机MAC地址也可以随机了
  10. 社会调查报告包括哪几个部分?
  11. 扬帆优配|高送转+高分红+高增长潜力股揭秘
  12. 项目管理-常见工具和技术总结
  13. PR中直接打开AE去做特效方法?
  14. 在eclipse中修改tomcat端口
  15. 2 软件测试之健壮性测试
  16. JavaScript函数式编程之副作用
  17. Vue指定日期选择框的值--自动计算过期时间
  18. Android逆向工程(一)-Apktool使用
  19. 概率论与数理统计(一)习题
  20. emu8086 不支持用 ? 来定义 数据

热门文章

  1. Elon Mask又搞大事情:新公司要将人脑与机器连接,给大脑上传想法不再是科幻...
  2. 多目录下一次性修改多个文件里的相同内容
  3. Web性能瓶颈查找经验总结
  4. Highlighter与BooleanQuery查询
  5. Node开发项目管理工具 Grunt 对比 Gulp
  6. Python 基础---列表
  7. Linux 字符集问题
  8. 第一天开博,想和大家认识。
  9. Star-shaped polygon
  10. java linux root权限管理_Linux--开启root用户并允许管理员登录