当你点击UITableView中的一行时,该行会高亮显示并被选中。是否有可能禁用这个,所以点击一行什么都不做?
当前回答
禁用UItableviewcell的高亮显示
cell.selectionStyle = UITableViewCellSelectionStyleNone;
并且不应该允许用户与单元格交互。
cell.userInteractionEnabled = NO;
其他回答
你只需要把这段代码放到cellForRowAtIndexPath中
禁用单元格的选择属性:(在点击单元格时)。
cell.selectionStyle = UITableViewCellSelectionStyle.None
根据我自己的实践经验,总结一下我认为的正确答案:
如果你想禁用部分单元格的选择,请使用:
cell.userInteractionEnabled = NO;
除了阻止选择,这也会阻止设置了tableView:didSelectRowAtIndexPath的单元格被调用。(感谢Tony Million的回答,谢谢!)
如果你的单元格中有需要点击的按钮,你需要改为:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
并且你还需要忽略单元格中的任何点击- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath。
如果你想禁用整个表的选择,使用:
tableView.allowsSelection = NO;
(感谢保罗·德·巴罗斯!)
objective - c:
Below snippet disable highlighting but it also disable the call to didSelectRowAtIndexPath. So if you are not implementing didSelectRowAtIndexPath then use below method. This should be added when you are creating the table. This will work on buttons and UITextField inside the cell though. self.tableView.allowsSelection = NO; Below snippet disable highlighting and it doesn't disable the call to didSelectRowAtIndexPath. Set the selection style of cell to None in cellForRowAtIndexPath cell.selectionStyle = UITableViewCellSelectionStyleNone; Below snippet disable everything on the cell. This will disable the interaction to buttons, textfields: self.tableView.userInteractionEnabled = false;
迅速:
以下是上述Objective-C解决方案的Swift等效版本:
更换第一个溶液 self.tableView.allowsSelection = false 更换第二溶液 细胞?。selectionStyle = UITableViewCellSelectionStyle。没有一个 更换第三种溶液 self.tableView.userInteractionEnabled = false
如果你想选择只闪光,而不是保持在选择状态,你可以调用,在
didSelectRowAtIndexPath
以下
[tableView deselectRowAtIndexPath:indexPath animated:YES];
它会刷新选中的状态并恢复。
我也一直在与这个相当丰富的斗争,在我的UITableViewCell中有一个控件禁止使用userInteractionEnabled属性。我有一个3单元静态表的设置,2与日期,1与开/关开关。在Storyboard/IB中,我成功地使底部不可选,但当你点击它时,从顶部行之一的选择消失了。这是我设置UITableView的WIP图像:
如果你点击第三行什么都没有发生,选择将停留在第二行。该功能实际上是苹果日历应用程序添加事件时间选择屏幕的副本。
代码是令人惊讶的兼容,一直到IOS2 =/:
- (NSIndexPath *)tableView: (UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 2) {
return nil;
}
return indexPath;
}
这与设置选择样式为none一起工作,因此单元格不会在触摸事件时闪烁
推荐文章
- 如何删除默认的导航栏空间在SwiftUI导航视图
- 如何在iOS中使用Swift编程segue
- Swift -整数转换为小时/分钟/秒
- Swift:声明一个空字典
- 在成功提交我的应用程序后,“太多符号文件”
- 首先添加一个UIView,甚至是导航栏
- 我如何改变UIButton标题颜色?
- 如何从UIImage (Cocoa Touch)或CGImage (Core Graphics)获取像素数据?
- 在Swift中如何调用GCD主线程上的参数方法?
- NSLayoutConstraints是可动画的吗?
- iOS -构建失败,CocoaPods无法找到头文件
- CFNetwork SSLHandshake iOS 9失败
- 请求失败:不可接受的内容类型:文本/html使用AFNetworking 2.0
- 缺少推荐的图标文件-该包不包含iPhone / iPod Touch的应用程序图标,像素为“120x120”,png格式
- 以编程方式创建segue