当您点击 UITableView
中的一行时,该行会突出显示并被选中。是否可以禁用此功能,因此点击一行什么也不做?
selection
,当然 highlighting
也会因此而被禁用。许多高票数的答案解释了如何disable selection
并且没有解决禁用 highlighting
的问题。要仅禁用突出显示,请使用 cell.selectionStyle = .None
或转到 storyboard / cell / Selection = None
您所要做的就是使用以下任一方法在 UITableViewCell
实例上设置选择样式:
目标-C:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
或者
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
斯威夫特 2:
cell.selectionStyle = UITableViewCellSelectionStyle.None
斯威夫特 3 和 4.x:
cell.selectionStyle = .none
此外,请确保您没有在表格视图委托中实现 -tableView:didSelectRowAtIndexPath:
,或者如果您确实实现了它,则明确排除您不希望有任何操作的单元格。
对我来说,以下工作正常:
tableView.allowsSelection = false
这意味着 didSelectRowAt#
根本不起作用。也就是说,触摸表格的一行,这样,绝对不会做任何事情。 (因此,很明显,永远不会有选定的动画。)
(请注意,如果在单元格上,您有 UIButton
或任何其他控件,当然这些控件仍然可以工作。您碰巧在表格单元格上拥有的任何控件都与 UITableView 允许您“选择”的能力完全无关一行”,使用 didSelectRowAt#
。)
还有一点需要注意的是:当 UITableView
处于编辑模式时,这不起作用。要在编辑模式下限制单元格选择,请使用以下代码:
tableView.allowsSelectionDuringEditing = false
selectionStyle
属性:在 Swift 中为 cell.selectionStyle = UITableViewCellSelectionStyle.None
,在 Objective-C 中为 cell.selectionStyle = UITableViewCellSelectionStyleNone
。
tableView.allowSelection = false
tableViewDidSelectRowAtIndexPath
,则需要 cell.selectionStyle = .None
。如果您不想或不关心调用该委托函数(即在 tableViewCell 中有一个文本字段)设置 tableView.allowsSelection = false
就可以了。
因为我最近阅读了这篇文章并且它对我有所帮助,所以我想发布另一个答案以整合所有答案(供后代使用)。
因此,根据您想要的逻辑和/或结果,实际上有 5 个不同的答案:
1.要禁用蓝色突出显示而不更改单元格的任何其他交互:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
当我有一个 UIButton - 或其他一些控件 - 托管在 UITableViewCell 中并且我希望用户能够与控件交互而不是单元格本身时,我会使用它。
注意:正如上面提到的 Tony Million,这不会阻止 tableView:didSelectRowAtIndexPath:
。我通过简单的“if”语句来解决这个问题,最常见的是测试该部分并避免对特定部分采取行动。
我想到的另一种测试单元格敲击的方法是:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// A case was selected, so push into the CaseDetailViewController
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.selectionStyle != UITableViewCellSelectionStyleNone) {
// Handle tap code here
}
}
2.要对整个表格执行此操作,您可以将上述解决方案应用于表格中的每个单元格,但您也可以这样做:
[tableView setAllowsSelection:NO];
在我的测试中,这仍然允许 UITableViewCell
内的控件进行交互。
3.使单元格“只读” ",你可以简单地这样做:
[cell setUserInteractionEnabled:NO];
4.使整个表“只读”
[tableView setUserInteractionEnabled:NO];
5.要即时确定是否突出显示单元格(根据this answer隐式包括选择),您可以实现以下UITableViewDelegate
协议方法:
- (BOOL)tableView:(UITableView *)tableView
shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
tableView:shouldHighlightRowAtIndexPath:
添加一个大喊大叫,这通常(并非总是)是我的首选方法。
总结一下我认为基于我自己的实施经验的正确答案:
如果您只想禁用某些单元格的选择,请使用:
cell.userInteractionEnabled = NO;
除了防止选择之外,这还停止了 tableView:didSelectRowAtIndexPath: 被设置为单元格的调用。 (此答案归功于 Tony Million,谢谢!)
如果单元格中有需要单击的按钮,则需要:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
并且您还需要忽略对 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
中单元格的任何点击。
如果要禁用整个表的选择,请使用:
tableView.allowsSelection = NO;
(感谢 Paulo De Barros,谢谢!)
tableView.allowsSelection = NO;
不允许在单元格内点击按钮...从那以后有什么变化吗?我知道这篇文章很旧
self
来设置其按钮的约束,而我应该一直使用 self.contentView。还要确保所有视图都从 contentView 的顶部垂直约束到底部。
从 iOS 6.0 开始,UITableViewDelegate
具有 tableView:shouldHighlightRowAtIndexPath:
。 (在 iOS Documentation 中了解它。)
此方法允许您将特定行标记为不可突出显示(并且隐含地,不可选择),而无需更改单元格的选择样式、使用 userInteractionEnabled = NO
或此处记录的任何其他技术弄乱单元格的事件处理。
您还可以通过从检查器窗格中的 selection
选项(UITableView 属性)中选择 NoSelection
来禁用界面构建器本身的行选择,如下图所示
https://i.stack.imgur.com/Qw1bX.png
SWIFT 3 的固定解决方案
cell.selectionStyle = .none
在 Attribute Inspector 的 UITableViewCell
的 XIB 中,将 Selection
的值设置为 None
。
https://i.stack.imgur.com/MHHe5.png
如果有人需要回答 Swift:
cell.selectionStyle = .None
如果你希望选择只闪烁,而不是保持在选中状态,你可以调用,在
didSelectRowAtIndexPath
以下
[tableView deselectRowAtIndexPath:indexPath animated:YES];
所以它会闪烁选定的状态并恢复。
如果您不想选择行,则可以从 UITableViewDelegate
协议使用方法 willSelectRowAtIndexPath
和 return nil
。
以同样的方式,您可以使用 willDeselectRowAtIndexPath
方法和 return nil
如果您不想取消选择该行。
这就是我使用的,在 cellForRowAtIndexPath
中编写此代码。:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
1- 您所要做的就是使用以下任一方法在 UITableViewCell
实例上设置 选择样式:
目标-C:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
或者
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
斯威夫特 2:
cell.selectionStyle = UITableViewCellSelectionStyle.None
斯威夫特 3:
cell.selectionStyle = .none
2 - 不要在您的表格视图 delegate
中实施 -tableView:didSelectRowAtIndexPath:
或明确排除您不希望执行的单元格(如果您实施了它)。
3 - 此外,您也可以从情节提要中进行。单击表格视图单元格,然后在表格视图单元格下的属性检查器中,将选择旁边的下拉列表更改为无。
4 - 您可以使用 (iOS) Xcode 9、Swift 4.0 中的以下代码禁用表格单元格突出显示
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "OpenTbCell") as! OpenTbCell
cell.selectionStyle = .none
return cell
}
目标-C:
下面的代码段禁用突出显示,但它也禁用对 didSelectRowAtIndexPath 的调用。所以如果你没有实现 didSelectRowAtIndexPath 然后使用下面的方法。这应该在您创建表时添加。不过,这将适用于单元格内的按钮和 UITextField。 self.tableView.allowsSelection = 否;下面的代码段禁用突出显示,它不会禁用对 didSelectRowAtIndexPath 的调用。在 cellForRowAtIndexPath cell.selectionStyle = UITableViewCellSelectionStyleNone 中将单元格的选择样式设置为无;下面的代码段禁用单元格上的所有内容。这将禁用与按钮、文本字段的交互: self.tableView.userInteractionEnabled = false;
迅速:
以下是上述 Objective-C
解决方案的 Swift
等效项:
替换第一个解决方案 self.tableView.allowsSelection = false 替换第二个解决方案单元格?.selectionStyle = UITableViewCellSelectionStyle.None 替换第三个解决方案 self.tableView.userInteractionEnabled = false
尝试输入:
cell.selected = NO;
它会在需要时取消选择您的行。
在 Swift3 ...
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let r = indexPath.row
print("clicked .. \(r)")
tableView.cellForRow(at: indexPath)?.setSelected(false, animated: true)
}
斯威夫特 3,4 和 5
更好的做法,在 UITableViewCell
中编写代码
例如,您有名称为 MyCell
的 UITableViewCell
,在 awakeFromNib
中只需写 self.selectionStyle = .none
完整示例:
class MyCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
self.selectionStyle = .none
}
}
我也一直在努力解决这个问题,在我的 UITableViewCell
中有一个控件禁止使用 userInteractionEnabled
属性。我有一个用于设置的 3 单元静态表,2 个带日期,1 个带开/关开关。在 Storyboard/IB 中玩过之后,我设法使底部的一个不可选择,但是当您点击它时,顶部行之一的选择消失了。这是我的设置 UITableView 的 WIP 图像:
https://i.stack.imgur.com/6qV3g.png
如果您点击第三行没有任何反应,则选择将保留在第二行。该功能实际上是 Apple 日历应用程序添加事件时间选择屏幕的副本。
该代码令人惊讶地兼容,一直到 IOS2 =/:
- (NSIndexPath *)tableView: (UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 2) {
return nil;
}
return indexPath;
}
这与将选择样式设置为无结合使用,因此单元格不会在触摸事件上闪烁
我们可以编写如下代码
cell.selectionStyle = UITableViewCellSelectionStyleNone;
但是当我们在上面有自定义单元格xib时,当时会发出警告
自定义单元格xib
我们需要从界面生成器中设置选择样式 None
您只需将此代码放入 cellForRowAtIndexPath
禁用单元格的选择属性:(点击单元格时)。
cell.selectionStyle = UITableViewCellSelectionStyle.None
我正在使用这个,它对我有用。
cell?.selectionStyle = UITableViewCellSelectionStyle.None
从 UITableViewDataSource 协议,在方法 cellForRowAt
内添加:
let cell = tableView.dequeueReusableCell(withIdentifier: "YOUR_CELL_IDENTIFIER", for: indexPath)
cell.selectionStyle = .none
return cell
或者
您可以转到 Storyboard > Select Cell > Identity Inspector > Selection 并从下拉列表中选择无。
尝试这个
cell.selectionStyle = UITableViewCellSelectionStyleNone;
和
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
您还可以使用 interfacebuilder 设置选择样式。
虽然这是防止行在选择期间显示突出显示的最佳和最简单的解决方案
cell.selectionStyle = UITableViewCellSelectionStyleNone;
我还想建议偶尔简短地显示该行已被选中然后将其关闭是有用的。这会提醒用户确认他们打算选择的内容:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
...
}
直接禁用 TableViewCell 突出显示到情节提要中
https://i.stack.imgur.com/SfVKW.png
禁用 UItableviewcell 的突出显示
cell.selectionStyle = UITableViewCellSelectionStyleNone;
并且不应允许用户与单元格进行交互。
cell.userInteractionEnabled = NO;
您也可以将背景颜色设置为清除以达到与 UITableViewCellSelectionStyleNone
相同的效果,以防您不想/不能使用 UITableViewCellSelectionStyleNone
。
您将使用如下代码:
UIView *backgroundColorView = [[UIView alloc] init];
backgroundColorView.backgroundColor = [UIColor clearColor];
backgroundColorView.layer.masksToBounds = YES;
[cell setSelectedBackgroundView: backgroundColorView];
当您向每个单元格添加额外的彩色视图时,这可能会降低您的性能。
您也可以从情节提要中执行此操作。单击表格视图单元格,然后在表格视图单元格下的属性检查器中,将选择旁边的下拉列表更改为无。
您可以使用 :
cell.selectionStyle = UITableViewCellSelectionStyleNone;
在 UITableView 的索引路径方法的行的单元格中。
您也可以使用:
[tableView deselectRowAtIndexPath:indexPath animated:NO];
在 tableview didselectrowatindexpath 方法中。
cell.selectionStyle = UITableViewCellSelectionStyle.none
。
你可以用这个
cell.selectionStyle = UITableViewCellSelectionStyleNone;
您可以使用 ....
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
不定期副业成功案例分享