【转】关于UItableViewCell的accessoryType属性
转载自:http://blog.csdn.net/kmyhy/article/details/6442351
使用的话,例如:
- cell.accessoryType = UITableViewCellAccessoryNone;//cell没有任何的样式
- cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//cell的右边有一个小箭头,距离右边有十几像素;
- cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//cell右边有一个蓝色的圆形button;
- cell.accessoryType = UITableViewCellAccessoryCheckmark;//cell右边的形状是对号;
对于 UITableViewCell 而言,其 accessoryType属性有4种取值:
UITableViewCellAccessoryNone,
UITableViewCellAccessoryDisclosureIndicator,
UITableViewCellAccessoryDetailDisclosureButton,
UITableViewCellAccessoryCheckmark
分别显示 UITableViewCell 附加按钮的4种样式:
无、
、
、
。
除此之外,如果你想使用自定义附件按钮的其他样式,必需使用UITableView 的 accessoryView 属性。比如,我们想自定义一个
的附件按钮,你可以这样做:
UIButton *button ;
if(isEditableOrNot){
UIImage *image= [UIImage imageNamed:@"delete.png"];
button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame;
[button setBackgroundImage:imageforState:UIControlStateNormal];
button.backgroundColor= [UIColor clearColor];
cell.accessoryView= button;
}else {
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor= [UIColor clearColor];
cell.accessoryView= button;
}
这样,当 isEditableOrNot 变量为 YES 时,显示如下视图:

但仍然还存在一个问题,附件按钮的事件不可用。即事件无法传递到 UITableViewDelegate 的accessoryButtonTappedForRowWithIndexPath 方法。
也许你会说,我们可以给 UIButton 加上一个 target。
好,让我们来看看行不行。在上面的代码中加入:
[button addTarget:self action:@selector(btnClicked:event:) forControlEvents:UIControlEventTouchUpInside];
然后实现btnClicked方法,随便在其中添加点什么。
点击每个 UIButton,btnClicked 方法中的代码被调用了!一切都是那么完美。
但问题在于,每个 UITableViewCell 都有一个附件按钮,在点击某个按钮时,我们无法知道到底是哪一个按钮发生了点击动作!因为addTarget 方法无法让我们传递一个区别按钮们的参数,比如 indexPath.row 或者别的什么对象。addTarget 方法最多允许传递两个参数:target和 event,而我们确实也这么做了。但这两个参数都有各自的用途,target 指向事件委托对象——这里我们把 self 即 UIViewController实例传递给它,event 指向所发生的事件比如一个单独的触摸或多个触摸。我们需要额外的参数来传递按钮所属的 UITableViewCell 或者行索引,但很遗憾,只依靠Cocoa 框架,我们无法做到。
但我们可以利用 event 参数,在 btnClicked 方法中判断出事件发生在UITableView的哪一个 cell 上。因为 UITableView 有一个很关键的方法 indexPathForRowAtPoint,可以根据触摸发生的位置,返回触摸发生在哪一个 cell 的indexPath。 而且通过 event 对象,我们也可以获得每个触摸在视图中的位置:
// 检查用户点击按钮时的位置,并转发事件到对应的accessorytapped事件
- (void)btnClicked:(id)senderevent:(id)event
{
NSSet *touches =[event allTouches];
UITouch *touch =[touches anyObject];
CGPointcurrentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath= [self.tableView indexPathForRowAtPoint:currentTouchPosition];
if (indexPath!= nil)
{
[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
}
}
这样,UITableView的accessoryButtonTappedForRowWithIndexPath方法会被触发,并且获得一个indexPath 参数。通过这个indexPath 参数,我们可以区分到底是众多按钮中的哪一个附件按钮发生了触摸事件:
-(void)tableView:(UITableView*)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath*)indexPath{
int* idx= indexPath.row;
//在这里加入自己的逻辑
⋯⋯
}
【转】关于UItableViewCell的accessoryType属性的更多相关文章
- uitableviewcell cell.accessoryType 右箭头
实现右侧的小灰色箭头 只要将cell的accessoryType属性设置为 UITableViewCellAccessoryDisclosureIndicator就可以了. 代码为:cell.acc ...
- (十五)UITableViewCell的常见属性
UItableViewCellStyle: typedef NS_ENUM(NSInteger, UITableViewCellStyle) { UITableViewCellStyleDefault ...
- 重写UITableViewCell子类中属性的setter方法来实现隐藏或显示该cell中的某些控件
重写UITableViewCell子类中属性的setter方法来实现隐藏或显示该cell中的某些控件 为什么会需要这样子的一种方法来实现隐藏或者显示一个cell中的某些控件呢? 其实,隐藏cell中某 ...
- iOS UITableViewCell AccessoryType属性
写了这么长时间的ios table 竟然不知道有一个Accessory来显示详情设置小图标,曾经竟然傻帽的取用一张图片来显示,如今想想真是..那痛啊.... cell.accessoryType = ...
- UITableViewCell的separatorInset属性
separatorInset这个属性是IOS7后才有的属性,所以需要判断一下,才能修改 if (IOS7_OR_LATER) { cell.separatorInset = UIEdgeInsetsZ ...
- iOS边练边学--UITableViewCell的常见属性设置
// 取消选中的样式(常用) 让当前 cell 按下无反应 cell.selectionStyle = UITableViewCellSelectionStyleNone; // 设置选中的背景色,U ...
- IOS客户端Coding项目记录导航
IOS客户端Coding项目记录(一) a:UITextField设置出现清除按键 b:绘画一条下划线 表格一些设置 c:可以定义表头跟底部视图(代码接上面) d:隐藏本页的导航栏 e:UIEdge ...
- IOS客户端Coding项目记录(二)
9:第三方插件整理 JSON转实体:jsonModel https://github.com/icanzilb/JSONModel/ 美化按键:BButton https://github.com/m ...
- ios cell常用属性
1.设置UITableViewCell的accessoryView 有时候我们需要设置cell的一些样式,比如下图, 这个就是设置了cell的accessory属性的内容,如果我们想在上面显示Swit ...
随机推荐
- VisualStudio中的代码段
VS很强大,在这里就不过多说了,在平时码代码时应用代码段会提高我们的编写速度. 举个例子: 比如输入Console.WriteLine (); 传统方法就是一个字母一个字母的输入进去. 如果大家掌握了 ...
- css3动画工具
去年,我刚刚开始学习css3时候,看到了腾讯的这个工具,引起了我对css3的兴趣. 配合着书本上的知识写了一些效果,感觉不错. http://www.f2e.name/case/css3/tools. ...
- Android UI高级交互设计Demo
首先:是google的新标准 Google Material design 开源项目 1.直接拿来用!十大Material Design开源项目 2.收集android上开源的酷炫的交互动画和视觉效果 ...
- select2简单例子
1.html中静态值 html <%--multiple 为多选--%> <select multiple id="e1"> <option>& ...
- CXF 调用C#.net的WebService
原文链接:http://hi.baidu.com/pengfeiiw/blog/item/3203e29065aa3a8aa977a4d0.html 1.编写C#.net的WebService Ser ...
- oracle 插入含&字符串
1.创建表 SQL> create table t(id number,name varchar2(20)); 表已创建. 2.常规方式插入 SQL> insert into t valu ...
- ASP.NET MVC 必备知识点杂谈
一 工程结构4个程序集 Microsoft.Web.Mvc --一些可以使用的,不确定的程序包System.Web.Mvc --主程序库下面两个列入3.5的Net框架了System.Web.Abs ...
- 将窗口置顶的方法:SetWindowPos、AttachThreadInput、SwitchToThisWindow
将窗口置顶的方法:SetWindowPos.AttachThreadInput.SwitchToThisWindow [转]http://hi.baidu.com/neil_danky/item/f9 ...
- python使用mysql的三个模块:mysql.connector、sqlalchemy、MySQLdb
在python中使用mysql其实很简单,只要先安装对应的模块即可,那么对应的模块都有什么?官方也没指定也没提供,pcat就推荐自己遇到的3个模块:mysql.connector.sqlalchemy ...
- python 关于dict的一些总结
总结了一些关于字典的小技巧或者注意的地方. 使用zip创建字典 创建字典有以下三种方法 dict(a=1, b=2, c=2) dict([(a,1), (b,2), (c,3)]) dict({a: ...