我在A类中有UITableView,在B类中有自定义视图。我想要的是 - 从B类添加自定义视图到A类视图,然后删除该视图并显示表。
我确实添加了视图和逻辑来删除该视图,但出于某种原因,我的tableView单元格没有响应触摸。
以下是我创建和添加视图(B类)的方法:
-(id)initWithFrame:(CGRect)frame andRemoveTableView:(UITableView*)tableView{
self = [super initWithFrame:frame];
NSLog(@"Calld");
self.backgroundColor = [UIColor whiteColor];
[self addLabel];
tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
return self;
}
现在实施A类:
@implementation MainTableViewController{
InitialView *initView;
BOOL isInitialViewTapped;
}
-(void)viewDidLoad{
// InitialView *initView = [[InitialView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
// self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
initView = [[InitialView alloc]initWithFrame:[[UIScreen mainScreen]bounds] andRemoveTableView:self.tableView];
[self.view addSubview:initView];
// [initView removeFromSuperview];
//Notification Center
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(recieveTap:)
name:@"Tap"
object:nil ];
[self isViewTapped];
}
-(void)isViewTapped{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
tap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tap];
}
-(void)viewTapped:(UIGestureRecognizer*)recognizer{
[[NSNotificationCenter defaultCenter] postNotificationName:@"Tap" object:self];
isInitialViewTapped = YES;
}
#pragma mark - table view delegate
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Fill data
InitialData *data = [InitialData new];
data.isFirstCall = YES;
//Show cell
myCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];
cell.taskText.text = [data.dataArray objectAtIndex:indexPath.row];
cell.timeStartedText.text = [self dateStr];
return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 3;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"SELECT");
[self performSegueWithIdentifier:@"push" sender:self];
}
#pragma mark - Notification Center
-(void)recieveTap:(NSNotification*)notification{
if (!isInitialViewTapped){
[initView removeFromSuperview];
if ([[notification name] isEqualToString:@"Tap"])
} else {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
}
非常简单。但是,当我点击屏幕并使我的自定义视图解除时,当我点击单元格segue不起作用。实际上,方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"SELECT");
[self performSegueWithIdentifier:@"push" sender:self];
}
甚至不打印NSLog。如何解决?
答案 0 :(得分:1)
UITableViewCell与UITapGestureRecognizer冲突。
您可以在调用 - (void)viewTapped:(UIGestureRecognizer *)识别器时删除手势。
如果你想保持手势,你应该设置手势代表。
-(void)isViewTapped{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
tap.delegate = self; // add this line
tap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tap];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
// TableViewCell, do not handle it
if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
return NO;
}
return YES;
}