UITableView的AccessoryButton自定义视图与触发被点击的事件
2014-10-02 02:09
357 查看
自定义UITableViewCell的accessory样式 默认的accessoryType属性有四种取值:UITableViewCellAccessoryNone、
UITableViewCellAccessoryDisclosureIndicator、 UITableViewCellAccessoryDetailDisclosureButton、 UITableViewCellAccessoryCheckmark。
如果想使用自定义附件按钮的其他样式,则需使用UITableView的accessoryView属性来指定。
[cpp] view
plaincopy
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:image forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
}else{
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
}
以上代码仅仅是定义了附件按钮两种状态下的样式,问题是现在这个自定义附件按钮的事件仍不可用。
即事件还无法传递到 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对象,正好也可以获得每个触摸在视图中的位置。
[cpp] view
plaincopy
// 检查用户点击按钮时的位置,并转发事件到对应的accessory tapped事件
- (void)btnClicked:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
if(indexPath != nil)
{
[self tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
}
}
这样,UITableView的accessoryButtonTappedForRowWithIndexPath方法会被触发,并且获得一个indexPath参数。通过这个indexPath参数,我们即可区分到底哪一行的附件按钮发生了触摸事件。
UITableViewCellAccessoryDisclosureIndicator、 UITableViewCellAccessoryDetailDisclosureButton、 UITableViewCellAccessoryCheckmark。
如果想使用自定义附件按钮的其他样式,则需使用UITableView的accessoryView属性来指定。
[cpp] view
plaincopy
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:image forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
}else{
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
}
以上代码仅仅是定义了附件按钮两种状态下的样式,问题是现在这个自定义附件按钮的事件仍不可用。
即事件还无法传递到 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对象,正好也可以获得每个触摸在视图中的位置。
[cpp] view
plaincopy
// 检查用户点击按钮时的位置,并转发事件到对应的accessory tapped事件
- (void)btnClicked:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
if(indexPath != nil)
{
[self tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
}
}
这样,UITableView的accessoryButtonTappedForRowWithIndexPath方法会被触发,并且获得一个indexPath参数。通过这个indexPath参数,我们即可区分到底哪一行的附件按钮发生了触摸事件。
相关文章推荐
- iOS中UITableView的cell点击事件不触发didSelectRowAtIndexPath(汇总)
- iOS中UITableView的cell点击事件不触发didSelectRowAtIndexPath
- iOS中UITableView的cell点击事件不触发didSelectRowAtIndexPath
- UIbutton作为UIimageView的子视图 不能触发点击事件原因
- IOS UItableView 滚动到底 触发事件
- UITableView增加自定义Button并绑定点击事件
- Android Launcher 解决BubbleTextView 点击事件只在ICON上面触发,防止误触
- iPhone开发-ios7环境下Uitableview删除某一行后忽略点击事件
- UIView添加手势 然后UITableView 添加进这个View 导致UITableView 的单元格点击事件无效
- UITableView DeleteControl ---- 响应UITableView编辑状态点击deleteControl的事件
- 【IOS学习笔记】UITableView 点击隐藏键盘 且不影响其他事件
- 给UITableView添加UITapGestureRecognizer 每个Item多事件点击出现崩溃
- 设置UITableView背景透明/监听cell左边的删除按钮的点击事件
- iOS 疑难杂症(什么鬼系列)— — UITableView 添加 tableFooterView 旋转屏幕后收不到点击事件!!!
- android 解决GestureDetector.SimpleOnGestureListener中对某些view,点击之后总是触发longPress事件
- IOS7 UITableView一行滑动删除后 被删除行的下一行的点击事件将被忽略解决办法
- IOS -- 对UITableView的backgroundview的研究(ios7中backgroundView点击事件不响应)
- view.performClick()触发点击事件
- UITableView上button的点击事件(一)
- 求助: Android 加载 webview, 点击webview 中网页时间, 如何触发 Android 事件