无法单击 UI 查询视图中的 UICollectionViewCell



假设我有一个视图控制器OptionsToCreateViewController,它继承了UICollectionViewController,并且我在另一个类似的视图控制器中使用了该视图控制器。。我可以看到视图,但无法点击单元格

self.optionsToCreateViewController = [[OptionsToCreateViewController alloc] init];
self.optionsToCreateViewController.view.translatesAutoresizingMaskIntoConstraints = FALSE;
[self.optionsToCreateViewController didMoveToParentViewController:self];
[self addChildViewController:self.optionsToCreateViewController];
[self.view addSubview:self.optionsToCreateViewController.view];

OrganizerBottomBar *orgBB = [OrganizerBottomBar new];
CGRect applicationFrame = [[UIScreen mainScreen] bounds];
CGFloat viewWidth = applicationFrame.size.width / 2;
if(![MyUIResources isPhone]){
viewWidth = applicationFrame.size.width / 4;
}
[self.view addConstraints:@[
[NSLayoutConstraint constraintWithItem:self.optionsToCreateViewController.view attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:-(orgBB.frame.size.height + 75)],
[NSLayoutConstraint constraintWithItem:self.optionsToCreateViewController.view attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:-viewWidth]
]];

这是在另一个视图控制器中实现视图控制器的正确方法吗

在集合视图单元格中:

[button addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
self.contentView.userInteractionEnabled = FALSE;
self.userInteractionEnabled = TRUE;

我还为buttonPressed函数设置了一个断点,它没有命中。我也用过

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *view = [self->button hitTest:[self->button convertPoint:point fromView:self] withEvent:event];
if (view == nil) {
view = [super hitTest:point withEvent:event];
}
return view;
}
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
if ([super pointInside:point withEvent:event]) {
return YES;
}
return !self->button.hidden && [self->button pointInside:[self->button convertPoint:point fromView:self] withEvent:event];
}

但没有用。有人能帮我吗?我被严重卡住了

CGRect applicationFrame = [[UIScreen mainScreen] bounds];
self.view = [UIView new];
self.view.translatesAutoresizingMaskIntoConstraints = NO;
self.view.backgroundColor = [UIColor colorWhite];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
self.collection = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
self.collection.translatesAutoresizingMaskIntoConstraints = NO;
self.collection.backgroundColor = [UIColor colorWhite];
self.collection.contentInset = UIEdgeInsetsMake(0, 7.0, 0, 16.0);
[self.collection registerClass:[OptionsToCreateCollectionViewCell class] forCellWithReuseIdentifier: [OptionsToCreateCollectionViewCell cellIdentifier]];
[self.collection setDataSource:self]; // UICollectionViewDataSource
[self.collection setDelegate:self]; // UICollectionViewDelegate

[self.view addSubview:self.collection];

CGFloat viewWidth = applicationFrame.size.width;
CGFloat frameWidth = viewWidth;
if(![MyUIResources isPhone]){
frameWidth = viewWidth/2;
}
[self.view addConstraints:
@[
[NSLayoutConstraint constraintWithItem:self.collection attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10],
[NSLayoutConstraint constraintWithItem:self.collection attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:20],
[NSLayoutConstraint constraintWithItem:self.collection attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:frameWidth],
[NSLayoutConstraint constraintWithItem:self.collection attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:114*2.5]
]
];

按钮的超视图是什么?在cell.contentView上添加按钮,而不是cell self。

1.检查按钮框(如果外部单元格不工作,则任何与该按钮重叠的视图都不工作(和用户交互是否启用

否则2.检查单元格框架和高度(检查集合视图内是否有(

否则3.集合视图超级框架设置正确(集合视图框架需要在超级视图内部(如果框架工作正确,则指定clipseToBounds=YESself.yourview.clipsToBounds=是;

注意:超过集合中的Action按钮后,选择了editem。然后检查是否工作,因为容易识别的问题只基于那个按钮或那个类。

最新更新