UITableViewCell中自定义cell添加手势没有响应解决方案
2017-06-05 16:37
417 查看
情况一:自定义UITableViewCell,且cell中添加了一个UILabel,我们的目的是给该label添加一个手势。
情况二:原生cell上添加了一个UIView,我们的目的是给UIView上的lable添加一个手势。
但是以上两种情况如果按照常规的添加方法,发现所添加的手势并不能响应。以下为解决方法:将手势添加到UITableView上或者添加到UIView上(即使添加到lable的父视图上),两种情况的解决方案类似,以下附上第一种情况的代码:(代码有点乱,还望海涵)
@interface TestViewController () <UITableViewDataSource, UITableViewDelegate>
@end
@implementation TestViewController {
UITableView *contentTableView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//初始化点击手势
UITapGestureRecognizer *tagGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
tagGesture.numberOfTapsRequired = 1;
contentTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
contentTableView.dataSource = self;
contentTableView.delegate = self;
//给tableView添加手势操作
[contentTableView addGestureRecognizer:tagGesture];
//或者给 UIview添加手势(前提你要创建)
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 50, 30)];
label.tag = 1;
[cell.contentView addSubview:label];
}
UILabel *label = (UILabel *)[cell.contentView viewWithTag:1];
label.text = [NSString stringWithFormat:@"text_%d", indexPath.row];
return cell;
}
#pragma mark - UITapGestureRecognizer
- (void)tapGesture:(UITapGestureRecognizer *)gesture {
//获得当前手势触发的在UITableView中的坐标
CGPoint location = [gesture locationInView:contentTableView];
//获得当前坐标对应的indexPath
NSIndexPath *indexPath = [contentTableView indexPathForRowAtPoint:location];
if (indexPath) {
//通过indexpath获得对应的Cell
UITableViewCell *cell = [contentTableView cellForRowAtIndexPath:indexPath];
//获得添加到cell.contentView中的UILabel
//---------------------------------------------
UILabel *label = nil;
for (UIView *view in cell.contentView.subviews) {
//for ( UIView *view in _perCardView.subviews)->自定义view
if ([view isKindOfClass:[UILabel class]]) {
label = (UILabel *)view;
//lable = (UILel *)[自定义view viewWithTag:2];
break;
}
}
//获得当前手势点击在UILabe中的坐标
CGPoint p = [gesture locationInView:label];
//CGPoint p = [gesture locationInView:自定义view];
//看看手势点的坐标是不是在UILabel中
if (CGRectContainsPoint(label.frame, p)) {
NSLog(@"label text : %@", label.text);
}
//---------------------------------------------
}
}
情况二:原生cell上添加了一个UIView,我们的目的是给UIView上的lable添加一个手势。
但是以上两种情况如果按照常规的添加方法,发现所添加的手势并不能响应。以下为解决方法:将手势添加到UITableView上或者添加到UIView上(即使添加到lable的父视图上),两种情况的解决方案类似,以下附上第一种情况的代码:(代码有点乱,还望海涵)
@interface TestViewController () <UITableViewDataSource, UITableViewDelegate>
@end
@implementation TestViewController {
UITableView *contentTableView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//初始化点击手势
UITapGestureRecognizer *tagGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
tagGesture.numberOfTapsRequired = 1;
contentTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
contentTableView.dataSource = self;
contentTableView.delegate = self;
//给tableView添加手势操作
[contentTableView addGestureRecognizer:tagGesture];
//或者给 UIview添加手势(前提你要创建)
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 50, 30)];
label.tag = 1;
[cell.contentView addSubview:label];
}
UILabel *label = (UILabel *)[cell.contentView viewWithTag:1];
label.text = [NSString stringWithFormat:@"text_%d", indexPath.row];
return cell;
}
#pragma mark - UITapGestureRecognizer
- (void)tapGesture:(UITapGestureRecognizer *)gesture {
//获得当前手势触发的在UITableView中的坐标
CGPoint location = [gesture locationInView:contentTableView];
//获得当前坐标对应的indexPath
NSIndexPath *indexPath = [contentTableView indexPathForRowAtPoint:location];
if (indexPath) {
//通过indexpath获得对应的Cell
UITableViewCell *cell = [contentTableView cellForRowAtIndexPath:indexPath];
//获得添加到cell.contentView中的UILabel
//---------------------------------------------
UILabel *label = nil;
for (UIView *view in cell.contentView.subviews) {
//for ( UIView *view in _perCardView.subviews)->自定义view
if ([view isKindOfClass:[UILabel class]]) {
label = (UILabel *)view;
//lable = (UILel *)[自定义view viewWithTag:2];
break;
}
}
//获得当前手势点击在UILabe中的坐标
CGPoint p = [gesture locationInView:label];
//CGPoint p = [gesture locationInView:自定义view];
//看看手势点的坐标是不是在UILabel中
if (CGRectContainsPoint(label.frame, p)) {
NSLog(@"label text : %@", label.text);
}
//---------------------------------------------
}
}
相关文章推荐
- UITableViewCell中自定义cell上UILabel添加手势没有响应解决方案
- UITableViewCell中的UILabel添加手势没有响应的解决方法
- 解决点击UITableViewCell时和自定义添加的手势冲突的问题
- 自定义cell,添加uilable,并且给lable添加tap手势,不响应的问题,对应解决方法
- 在UITableView或UICollectionView的自定义的cell里添加手势或button,响应方法时如何传参数
- android--解决方案--自定义tabhost(动态添加选项+带自动水平滑动选项卡+手势切换选项卡及内容功能)
- iOS解决UIcollectionView添加手势后不响应其cell的问题
- 根据点击头像的手势获取自定义Cell 的属性, UITableViewCell, 头像, iOS
- UIWebView中添加手势不响应的解决方案
- 为 自定义 Cell 上的控件(Label、ImageView)添加手势
- 给UILabel添加自定义手势后没有反应
- C++中添加自定义消息的响应 (BEGIN_MESSAGE_MAP ON_MESSAGE)
- 在MFC中添加自定义消息响应
- 在消息栏内添加一个图标,通过自定义消息响应图标
- 在VC中 添加响应自定义消息
- IIS5.1错误,启动时WEB服务提示:服务器没有及时响应启动或控制请求 之终极解决方案。
- c++builder中添加和响应自定义消息
- Android中自定义ListView无法响应OnItemClickListener中的onItemClick方法问题解决方案
- “Visual Studio .NET 无法创建或打开应用程序。问题很可能是因为本地WEB本地服务器上没有安装所需的组件。请运行Visual Studio .NET安装程序并添加WEB开发组件“的一种解决方案
- QT中添加自定义Class,出现“undefined reference to vtable for”之解决方案(Eclipse+Linux)