【发布时间】:2011-04-05 22:08:34
【问题描述】:
如何禁止在 UITableView 中仅选择单个单元格?我有几个,我只想禁用最后一个。
【问题讨论】:
标签: ios iphone cocoa-touch uitableview
如何禁止在 UITableView 中仅选择单个单元格?我有几个,我只想禁用最后一个。
【问题讨论】:
标签: ios iphone cocoa-touch uitableview
如果有人想知道如何快速实现这一点,那么这是我的代码。我正在使用 Xcode 7 并使用 iPad Retina(iOS 9) 进行测试。
cell.selectionStyle = UITableViewCellSelectionStyle .None
cell.userInteractionEnabled = false
无论你想要,尝试放置这两行代码。就我而言,我已在此方法中使用它来显示单元格。
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
请记住,这两行代码会阻止对您的单元格进行任何类型的选择或交互,但您只能根据需要单独使用第一行。那就是……
cell.selectionStyle = UITableViewCellSelectionStyle .None
只有这一行会阻止选择您的单元格。
但是,第二行将使单元格“只读”。那就是..
cell.userInteractionEnabled = false
谢谢
希望这会有所帮助。
【讨论】:
快速尝试一下:
self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
【讨论】:
使用 iOS 6。
您可以使用以下委托方法并返回 NO 以防您不选择它,并返回 YES 以防您希望它被选择。
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath.section == 0;
}
【讨论】:
要停止选择某些单元格,请使用:
cell.userInteractionEnabled = NO;
除了阻止选择之外,这还会停止为设置了它的单元格调用 tableView:didSelectRowAtIndexPath:。它还将使画外音将其视为暗淡按钮(可能是也可能不是您想要的)。
请注意,如果单元格中有交互式元素(即开关/按钮),则需要使用 cell.selectionStyle = UITableViewCellSelectionStyleNone; 代替,然后确保忽略对 tableView:didSelectRowAtIndexPath: 中单元格的点击。
【讨论】:
cell.accessoryView,我已将其设置为我希望可点击的UIButton。
我发现的最干净的解决方案只使用委托方法 willDisplayCell。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if([indexPath row] == 0) //<-----ignores touches on first cell in the UITableView
{ //simply change this around to suit your needs
cell.userInteractionEnabled = NO;
cell.textLabel.enabled = NO;
cell.detailTextLabel.enabled = NO;
}
}
您不必在委托方法 didSelectRowAtIndexPath 中采取任何进一步的操作来确保忽略此单元格的选择。对此单元格的所有触摸都将被忽略,单元格中的文本也将变灰。
【讨论】:
将其放入您的自定义表 VC 中:
// cells lacking UITableViewCellAccessoryDisclosureIndicator will not be selectable
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType != UITableViewCellAccessoryDisclosureIndicator) {
return nil;
}
return indexPath;
}
// disabled cells will still have userinteraction enabled for their subviews
- (void)setEnabled:(BOOL)enabled forTableViewCell:(UITableViewCell *)tableViewCell
{
tableViewCell.accessoryType = (enabled) ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
// if you dont want the blue selection on tap, comment out the following line
tableViewCell.selectionStyle = (enabled) ? UITableViewCellSelectionStyleBlue : UITableViewCellSelectionStyleNone;
}
然后要启用/禁用 someTableViewCell 的选择,请执行以下操作:
[self setEnabled:state forTableViewCell:someTableViewCell];
大功告成,可以发货了。
【讨论】:
正如我提到的in another thread,上述所有方法都不能精确地解决问题。禁用单元格的正确方法是通过方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
在那个方法中,必须使用
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
禁用单元格选择,但仍然允许用户与单元格的子视图进行交互,例如 UISwitch。
【讨论】:
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self numberOfRowsInSection] == [indexPath row]) {
return nil;
} else {
return indexPath;
}
}
表格的最后一行不会被选中
【讨论】:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = ...
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
【讨论】:
tableView:didSelectRowAtIndexPath:。
cell.selectionStyle = .None 和cell.userInteractionEnabled = false 将涵盖大多数情况。