启用Tableview滚动但禁用触摸
我想禁用我的tableview。意味着没有按钮,TextFields和其他控件可以被触摸,但我也希望我的tableview滚动应该正常工作。启用Tableview滚动但禁用触摸
self.tableView.userInteractionEnabled=true;
我已经使用了这个,但这使得滚动也禁用。
您应该将要禁用UITableView
单元的单元的个人userInteractionEnabled
属性设置为NO
,而不是整个UITableView
。
例如,myButton.userInteractionEnabled = NO;
其设置为NO
在整个UITableView
禁用用于滚动任何手势识别。
只需将userInteractionEnabled
设置为假到您的UITableViewCell
即可。
func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
// do your stuffs
cell.userInteractionEnabled = false
return cell
}
要确定停用选择
tableView.allowSelection = false
这不会阻止对单元格的选择吗? –
@IsaRanjha是的正确thx我更新了我的答案。他没有提及选择,这就是为什么我没有触及 –
你必须设置只isUserInteractionEnabled
& selectionStyle
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.selectionStyle = .none;
cell.isUserInteractionEnabled = false;
return cell
}
无需从属性检查器的编码只需取消用户交互Enabled属性的任何一行您的手机
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellIdentifier", for: indexPath)
cell.selectionStyle = .none;
cell.isUserInteractionEnabled = false;
return cell
}
尝试覆盖的UITableView touchesShouldCancelInContentView
方法:
// OC代码:
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
//when you want to scroll and touch event don't deliver to controls.
if ([view isKindOfClass:[UIControl class])){
return YES;
}
return [super touchesShouldCancelInContentView:view];
}
您可以禁用选择,当你开始滚动。实施UIScrollViewDelegate
在scrollViewWillBeginDragging:
在scrollViewDidEndDragging:willDecelerate:
tableView.allowsSelection = YES;
您的视图只包含一个'tableView'? – kamwysoc
禁用你的单元格的用户交互而不是表格视图 –