我的问题是这个问题的超集——我在 UITableViewCells 中有 UISliders,而整个 UITableView 是 UIScrollView 中的一个页面。滑块对与其他两个的交互造成严重破坏,子类化解决方案不起作用。这是我想出的效果很好的方法:在滑块移动时发送通知,并在此期间启用 UITableView 和 UIScrollView disableScrolling。请注意下图中:我的滑块是水平的,我的 tableview 是垂直的,我的 UIScrollView 有水平页面。

UITableViewCell 为以编程方式创建的滑块拾取事件:
self.numberSlider = [[UISlider alloc] init];
[self.numberSlider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.numberSlider addTarget:self action:@selector(sliderTouchDown:) forControlEvents:UIControlEventTouchDown];
[self.numberSlider addTarget:self action:@selector(sliderTouchUp:) forControlEvents:UIControlEventTouchUpInside];
[self.numberSlider addTarget:self action:@selector(sliderTouchUp:) forControlEvents:UIControlEventTouchUpOutside];
就本教程而言,我们只关心 touchDown 和 Up:
- (void)sliderTouchDown:(UISlider *)sender
{
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFY_SLIDER_TOUCH_BEGAN object:nil];
}
- (void)sliderTouchUp:(UISlider *)sender
{
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFY_SLIDER_TOUCH_ENDED object:nil];
}
现在,我们在 UITableView 中都捕获到这些通知(请注意,tableview 在 VC 中,但我确信如果您进行子类化,这将起作用):
- (void)viewDidLoad
{
// other stuff
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sliderTouchDown:) name:NOTIFY_SLIDER_TOUCH_BEGAN object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sliderTouchUp:) name:NOTIFY_SLIDER_TOUCH_ENDED object:nil];
}
- (void)sliderTouchDown:(NSNotification *)notify
{
self.treatmentTableView.scrollEnabled = NO;
}
- (void)sliderTouchUp:(NSNotification *)notify
{
self.treatmentTableView.scrollEnabled = YES;
}
和 UIScrollView(同上,封装在一个 VC 中):
- (void)viewDidLoad
{
// other stuff
// Register for slider notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disableScrolling:) name:NOTIFY_SLIDER_TOUCH_BEGAN object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enableScrolling:) name:NOTIFY_SLIDER_TOUCH_ENDED object:nil];
}
- (void)disableScrolling:(NSNotification *)notify
{
self.scrollView.scrollEnabled = NO;
}
- (void)enableScrolling:(NSNotification *)notify
{
self.scrollView.scrollEnabled = YES;
}
我很想听听一个更优雅的解决方案,但这个绝对可以完成工作。当您使用滑块时,表格和滚动视图保持不动,当您在滑块外部单击时,表格视图和滚动视图按预期移动。另外 - 请注意,我可以在此解决方案中使用所有 3 个组件的非子类实例。希望这对某人有帮助!