Iphone UITableView将某些单元格设置为;“不可选择”;
如何将UITableView的单元格属性设置为不可选择?我不想在用户点击单元格时看到蓝色的选择框。苹果说,在didSelectRowAtIndexPath中,您应该做的第一件事是取消选择该行Iphone UITableView将某些单元格设置为;“不可选择”;,iphone,cocoa-touch,Iphone,Cocoa Touch,如何将UITableView的单元格属性设置为不可选择?我不想在用户点击单元格时看到蓝色的选择框。苹果说,在didSelectRowAtIndexPath中,您应该做的第一件事是取消选择该行 [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO]; - (void) tableView:(UITableView *)tableView willDisplayCell:(UITableV
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
然后,您可以将AccessoryType更改为选中标记或无,等等。因此,当您输入didSelectRowAtIndexPath时,您可以取消选择该行,如果不打算选择该行,则不必选中该行
防止行选择
要完全阻止选择UITableViewCell
,请让您的UITableViewDelegate
实现。如果不希望选择行,可以从该方法返回nil
- (NSIndexPath *)tableView:(UITableView *)tv willSelectRowAtIndexPath:(NSIndexPath *)path
{
// Determine if row is selectable based on the NSIndexPath.
if (rowIsSelectable) {
return path;
}
return nil;
}
这将阻止选择和调用该行。但是,请注意,这不会阻止该行高亮显示
防止行高亮显示
如果要防止触摸时该行以可视方式突出显示,可以确保将单元格的设置为UITableViewCellSelectionStyleNone
,或者最好让UITableViewDelegate执行以下操作:
- (BOOL)tableView:(UITableView *)tv shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
// Determine if row is selectable based on the NSIndexPath.
return rowIsSelectable;
}
将表格单元格的selectionStyle
属性设置为UITableViewCellSelectionStyleNone
。这将防止它突出显示,您还可以检查表视图中的属性:didselectRowatinexPath:
也有此问题,请尝试前面提到的所有内容。最后一个技巧是添加以下行,它消除了选择表格单元格时的“蓝色闪光”:
self.myTableView.allowsSelection = NO;
不确定是这一行还是所有的组合,但总的来说,我没有得到更多的蓝色选择,甚至没有蓝色闪光。快乐 使用以下命令:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
另一种方法是向UITableViewCell
添加两个类别方法。我比塞巴斯蒂安(同样好)更喜欢这个答案,因为我建桌子的方式。我想这可能对其他人有帮助
- (void)setSelectable:(BOOL)enabled {
[self setSelectionStyle:UITableViewCellSelectionStyleNone];
[self setUserInteractionEnabled:enabled];
}
- (BOOL)isSelectable {
BOOL disabled = [self selectionStyle]==UITableViewCellSelectionStyleNone &&
[self isUserInteractionEnabled];
return ! disabled;
}
设置cell.userInteractionEnabled=NO
如果您已经在Interface Builder中设计了单元格,那么您可以通过删除tableViewCell
的“启用用户交互”复选框来实现这一点
选择一个不希望显示为蓝色的单元格
然后选择属性检查器(侧面“属性”视图中标尺图标旁边的盾牌图标)
然后将“选择”字段从“蓝色”更改为“无”
注意,假设这仍然是选择,如果您只是想避免UI效果,它将不会显示为已选择 仅适用于iOS 6+版本
您可以实现该方法
tableView:shouldlhighlightrowatindexpath:
在您的代理中。
请阅读此处的详细信息:要取消选中某些行,必须对UITableView委托的两种方法进行一些更改 在下面的方法中
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
- (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) cell.selectionStyle =UITableViewCellSelectionStyleNone;
如果用户试图选择,上述代码将阻止突出显示单元格
在下面的委托方法中
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
- (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)返回代码>行仍将被选中,应用程序有可能崩溃使用IB也是一种优雅的方式:
使用tableView:willDisplayCell:ForRowatineXpath:而不是tableView:DidSelectRowatineXpath:来清除第一次触摸单元格时出现的闪光灯
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
Swift 4:
您可以使用UITableViewDelegate:shouldHighlightRowAt
此答案假设您创建了一个名为:CustomTableViewCell的自定义单元格
并且您在名为:isSelectable的自定义单元格中创建了一个布尔值
这是塞巴斯蒂安·塞利斯的objc答案的Swift版本。Swift 4:
在单元类中:
selectionStyle = .none
斯威夫特5
cell.isUserInteractionEnabled = false
肯德尔是对的。遵循塞巴斯蒂安·塞利斯的回答,首先防止调用DidSelectRowatineXpath。不过,您也应该设置selectionStyle以防止突出显示。是的,我认为Sebastian Celis的答案是正确的,感谢DanielStoryboard fans使用静态表视图可以选择UITableViewCell,在“属性检查器”下可以找到选择字段并将其设置为“无”。在这种情况下,当用户触摸单元格时,它将在短时间内高亮显示,然后取消高亮显示-但据我所知,关键是要完全停止高亮显示。问题是,这样做会使tableview中的所有单元格都无法选择。如果您希望某些单元格可选择,而其他单元格不可选择,则此操作无法完成。Sebastian的方法对我很有效。没错,AllowSelection=NO将禁用所有单元格的选择。Interface Builder中是否有此属性的等效属性?它只会阻止显示选择颜色,didSelectRowAtIndexPath
仍被调用。这与Daniel Dickison对此问题的回答完全相同。Swift 2.2更新--cell.selectionStyle=UITableViewCellSelectionStyle.none谢谢!这正是我所需要的。在我看来,这应该是公认的答案。它实际上解决了问题,而不是掩盖问题。请注意,在突出显示单元格后调用tableView:WillSelectRowatineXpath:,这将产生闪烁。根据苹果的描述:这个方法只有在用户触摸一排然后抬起手指时才会被调用;该行在此之前不会被选中,尽管它在触地时会高亮显示。我同意@simpleBob。要使用此解决方案,还需要在不可选择的行上将cell.selectionStyle设置为UITableViewCellSelectionStyleNone。否则它看起来很俗气。那么最好的答案仍然是公认的答案。您可以选择在didSelectRowAtIndexPath
中不执行任何操作,因为