UITableViewCell - accessoryType属性
来源:互联网 发布:美女写真软件 编辑:程序博客网 时间:2024/06/03 06:13
对于 UITableViewCell 而言,其 accessoryType属性有4种取值:
UITableViewCellAccessoryNone,
UITableViewCellAccessoryDisclosureIndicator,
UITableViewCellAccessoryDetailDisclosureButton,
UITableViewCellAccessoryCheckmark
分别显示 UITableViewCell 附加按钮的4种样式:
无、、、。
cell.accessoryType = UITableViewCellAccessoryNone;//cell没有任何的样式 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//cell的右边有一个小箭头,距离右边有十几像素; cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//cell右边有一个蓝色的圆形button; cell.accessoryType = UITableViewCellAccessoryCheckmark;//cell右边的形状是对号;
除此之外,如果你想使用自定义附件按钮的其他样式,必需使用UITableView 的 accessoryView 属性。比如,我们想自定义一个 的附件按钮,
你可以这样做:
UIButton *button ; if(isEditableOrNot) { <span style="white-space:pre"></span> UIImage *image= [UIImage imageNamed:@"delete.png"]; <span style="white-space:pre"></span> button = [UIButton buttonWithType:UIButtonTypeCustom]; <span style="white-space:pre"></span> CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);<span style="white-space:pre"></span> button.frame = frame;<span style="white-space:pre"></span> [button setBackgroundImage:imageforState:UIControlStateNormal];<span style="white-space:pre"></span> button.backgroundColor= [UIColor clearColor];<span style="white-space:pre"></span> cell.accessoryView= button; } else {<span style="white-space:pre"></span> button = [UIButton buttonWithType:UIButtonTypeCustom];<span style="white-space:pre"></span> button.backgroundColor= [UIColor clearColor];<span style="white-space:pre"></span> 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; //在这里加入自己的逻辑 ⋯⋯}
- iOS UITableViewCell AccessoryType属性
- UITableViewCell的accessoryType属性
- UITableViewCell的accessoryType属性
- UITableViewCell - accessoryType属性
- 关于uitableviewcell的accessoryType属性
- 关于uitableviewcell的accessoryType属性
- 关于uitableviewcell的accessoryType属性
- 关于uitableviewcell的accessoryType属性
- 关于uitableviewcell的accessoryType属性
- 关于uitableviewcell的accessoryType属性
- 关于UITableviewcell的accessoryType属性
- 关于uitableviewcell的accessoryType属性
- UITableViewCell的accessoryType属性复用
- UItableviewcell: accessoryType
- UITableViewCell的separatorInset属性
- UITableView属性 自定义UITableViewCell
- cell.accessoryType
- iOS:UITableViewCell 方法 属性 详解
- 如何快速制作漫画App?
- matlab中的数字滤波器
- Android动画-概述
- 入职实习第四天:ViewTreeObserver简介
- 【ASP.NET】初步了解
- UITableViewCell - accessoryType属性
- 你需要知道的Android拍照适配方案
- 十个值得一试的开源深度学习框架
- mongodb 2.4升级至3.2
- htmlunit模拟浏览器实现163邮箱自动登录
- 应聘时最漂亮的回答、回答问题技巧
- 《Android开发艺术》之View的事件体系(上)
- HDU 5699 货物运输
- leetcode 144. Binary Tree Preorder Traversal