【发布时间】:2009-05-01 17:45:40
【问题描述】:
如何将 UITableView 的单元格属性设置为不可选择?当用户点击单元格时,我不想看到那个蓝色的选择框。
【问题讨论】:
-
请注意最近的 googlers:从 iOS 6 及更高版本开始,执行此操作的方法是
tableView:shouldHighlightRowAtIndexPath:as noted by Ayush below
标签: iphone cocoa-touch
如何将 UITableView 的单元格属性设置为不可选择?当用户点击单元格时,我不想看到那个蓝色的选择框。
【问题讨论】:
tableView:shouldHighlightRowAtIndexPath: as noted by Ayush below
标签: iphone cocoa-touch
要完全防止选择UITableViewCell,请让您的UITableViewDelegate 实现tableView:willSelectRowAtIndexPath:。如果您不想选择该行,则可以通过该方法返回 nil。
- (NSIndexPath *)tableView:(UITableView *)tv willSelectRowAtIndexPath:(NSIndexPath *)path
{
// Determine if row is selectable based on the NSIndexPath.
if (rowIsSelectable) {
return path;
}
return nil;
}
这可以防止选择行和调用tableView:didSelectRowAtIndexPath:。但是请注意,这不会阻止该行被突出显示。
如果您想防止在触摸时突出显示该行,您可以确保将单元格的selectionStyle 设置为UITableViewCellSelectionStyleNone,或者最好让您的UITableViewDelegate 实现tableView:shouldHighlightRowAtIndexPath:,如下所示:
- (BOOL)tableView:(UITableView *)tv shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
// Determine if row is selectable based on the NSIndexPath.
return rowIsSelectable;
}
【讨论】:
didSelectRowAtIndexPath 中选择不执行任何操作,因为无论如何都会调用 willSelectRowAtIndexPath。
将表格单元格的selectionStyle 属性设置为UITableViewCellSelectionStyleNone。这应该会阻止它突出显示,您还可以在 tableView:didSelectRowAtIndexPath: 中检查该属性。
【讨论】:
使用这个:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
【讨论】:
didSelectRowAtIndexPath 仍会被调用。
仅适用于 iOS 6+。
您可以实现该方法
tableView:shouldHighlightRowAtIndexPath: 在您的代表中。
在这里阅读更多:http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html
【讨论】:
也有这个问题,尝试了已经提到的所有方法。在选择表格单元格时摆脱“蓝色闪烁”的最后一个技巧是添加以下行:
self.myTableView.allowsSelection = NO;
不确定是这一行还是所有内容的组合,但作为总的结果,我没有更多的蓝色选择,甚至没有蓝色闪光。快乐!
【讨论】:
设置cell.userInteractionEnabled = NO;
【讨论】:
使用 IB 也是一种优雅的方式:
【讨论】:
Apple 说你应该在 didSelectRowAtIndexPath 中做的第一件事是取消选择行
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];
然后您可以将 AccessoryType 更改为复选标记或无等。因此,当您输入 didSelectRowAtIndexPath 时,您可以取消选择该行,如果它不应该被选中,则根本不检查该行。
【讨论】:
另一种方法是向UITableViewCell 添加几个类别方法。我比塞巴斯蒂安(也很好)的回答更喜欢这个,因为我建立桌子的方式。我认为这可能对其他人有帮助。
- (void)setSelectable:(BOOL)enabled {
[self setSelectionStyle:UITableViewCellSelectionStyleNone];
[self setUserInteractionEnabled:enabled];
}
- (BOOL)isSelectable {
BOOL disabled = [self selectionStyle]==UITableViewCellSelectionStyleNone &&
[self isUserInteractionEnabled];
return ! disabled;
}
【讨论】:
斯威夫特 4:
您可以使用 UITableViewDelegate 防止选择和突出显示:shouldHighlightRowAt
此答案假定您创建了一个名为:CustomTableViewCell 的自定义单元格
并且您在该自定义单元格中创建了一个布尔值,名为:isSelectable
func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
let cell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
if cell.isSelectable == false {
return false
} else {
return true
}
}
这是 Sebastian Celis 的 objc 答案的 Swift 版本。
【讨论】:
如果您在 Interface Builder 中设计了单元格,则可以通过从 tableViewCell 的“User Interaction Enabled”中删除复选框来完成此操作。
【讨论】:
斯威夫特 5
cell.isUserInteractionEnabled = false
【讨论】:
还有另一种简单的方法可以避免选择显示为蓝色。
选择一个您不想显示为蓝色的单元格。
然后选择属性检查器(侧面属性视图中标尺图标旁边的盾牌图标)。
然后将“选择”字段从“蓝色”更改为“无”。
注意,大概这仍在选择中,如果您只想避免 UI 效果,它就不会显示为选中状态。
【讨论】:
使用 tableView: willDisplayCell: forRowAtIndexPath: 代替 tableView: didSelectRowAtIndexPath: 以消除第一次触摸单元格时出现的闪光。
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
【讨论】:
要取消选中某些行,您必须对 UITableView 委托的两种方法进行一些更改。
在下面的方法中
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
分配完单元格后,写下面的代码
if(indexPath.row == someCellNumber) cell.selectionStyle =UITableViewCellSelectionStyleNone;
如果用户以某种方式尝试选择,上述代码将阻止突出显示单元格。
在下面的这个委托方法中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if(indexPath.row == someCellNumber) return;
//for other cells write the code below...
}
如果你不写if(indexPath.row == someCellNumber) return; 行仍然会被选中,并且有可能导致应用崩溃
【讨论】:
斯威夫特 4:
在单元格类中:
selectionStyle = .none
【讨论】: