您的位置:首页 > 移动开发 > IOS开发

IOS应用开发03——自定义Cell的AccessoryButton及事件源被点击的cell行

2014-06-17 17:45 691 查看
写了个UITableView,要用AccessoryButton,遗憾的是官方给的样式比较少,如下图所示。


想自定义一下样式,用了下面的代码:

UIImage *image= [ UIImage imageNamed:@"man" ];
UIButton *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 ];
[button addTarget:self
action:@selector(accessoryButtonIsTapped:event:)
forControlEvents:UIControlEventTouchUpInside];


实现了效果:



可之后发现
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{}方法不管用了。为了得到这个事件源,知道是那行被点击了,煞费苦心,最后还是在一位大神的文章:(/article/4864086.html)中找到了解决问题的方式,真心感谢。方法如下:
- (void)accessoryButtonIsTapped:(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];
}
}
留以备忘并希望给遇到同样问题的人参考。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐