ios 禁用单个 UITableViewCell 的选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3603315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable selection of a single UITableViewCell
提问by DexterW
How do you disable selecting only a single cell in a UITableView? I have several, and I only want the last to be disabled.
如何禁用仅选择 UITableView 中的单个单元格?我有几个,我只想禁用最后一个。
回答by Camilo Sanchez
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = ...
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
回答by JosephH
To stop just some cells being selected use:
要停止仅选择某些单元格,请使用:
cell.userInteractionEnabled = NO;
As well as preventing selection, this also stops tableView:didSelectRowAtIndexPath: being called for the cells that have it set. It will also make voiceover treat it the same as a dimmed button (which may or may not be what you want).
除了阻止选择外,这还会阻止 tableView:didSelectRowAtIndexPath: 为设置了它的单元格调用。它还会使画外音将其视为变暗的按钮(这可能是您想要的,也可能不是)。
Note that if you have interactive elements in the cell (ie. switches/buttons), you'll need to use cell.selectionStyle = UITableViewCellSelectionStyleNone;
instead and then make sure to ignore taps on the cell in tableView:didSelectRowAtIndexPath:
.
请注意,如果您在单元格中有交互元素(即开关/按钮),您将需要使用cell.selectionStyle = UITableViewCellSelectionStyleNone;
,然后确保忽略在tableView:didSelectRowAtIndexPath:
.
回答by DexterW
Throw this in your custom Table VC:
将其放入您的自定义 Table 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;
}
Then to enable/disable selection for someTableViewCell, do this:
然后要启用/禁用 someTableViewCell 的选择,请执行以下操作:
[self setEnabled:state forTableViewCell:someTableViewCell];
You're done and can ship.
大功告成,可以发货了。
回答by Aaron Saunders
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self numberOfRowsInSection] == [indexPath row]) {
return nil;
} else {
return indexPath;
}
}
the last row of the table will not be selected
表格的最后一行不会被选中
回答by MightyMouse
As I mentioned in another threadall the above methods are not solving the problem precisely. The correct way of disabling a cell is through the method
正如我在另一个线程中提到的,上述所有方法都不能准确地解决问题。禁用单元格的正确方法是通过方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
and in that method one has to use
在那种方法中必须使用
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
which disables cell selection but stillallows the user to interact with subviews of the cell such as a UISwitch.
这会禁用单元格选择,但仍允许用户与单元格的子视图(例如 UISwitch)进行交互。
回答by Scooter
The cleanest solution that I have found to this only makes use of the delegate method willDisplayCell.
我发现的最干净的解决方案只使用委托方法 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;
}
}
You don't have to take any further action in the delegate method didSelectRowAtIndexPath to ensure that the selection of this cell is ignored. All touches on this cell will be ignored and the text in the cell will be grayed out as well.
您不必在委托方法 didSelectRowAtIndexPath 中采取任何进一步的操作来确保忽略此单元格的选择。此单元格上的所有触摸都将被忽略,并且单元格中的文本也将变灰。
回答by mohamede1945
with iOS 6.
使用 iOS 6。
You can use the following delegate method and return NO
in case you don't it to be selected and YES
in case you want it to be selected.
您可以使用下面的委托方法,并返回NO
在你没有它被选中的情况下,并YES
在情况下,你希望它被选中。
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath.section == 0;
}
回答by Masa S-AiYa
Try this in swift:
快速试试这个:
self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
回答by onCompletion
If anyone wondering how to achieve this in swift then here is my code. I am using Xcode 7 and tested using iPad Retina(iOS 9).
如果有人想知道如何快速实现这一点,那么这是我的代码。我正在使用 Xcode 7 并使用 iPad Retina(iOS 9)进行测试。
cell.selectionStyle = UITableViewCellSelectionStyle .None
cell.userInteractionEnabled = false
Try to place this two line code whether you want. In my case I have used this in this method for displaying cells.
无论您愿意,都可以尝试放置这两行代码。就我而言,我在此方法中使用它来显示单元格。
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
Remember this two line code will block any kind of selection or interaction to your cells but you can only use the first line individually if you want. That is...
记住这两个线路代码将阻止任何一种选择或互动,您的细胞,但如果你愿意,你可以只单独使用的第一道防线。那是...
cell.selectionStyle = UITableViewCellSelectionStyle .None
Only this line will block the selection to your cells.
只有这一行会阻止对单元格的选择。
However the second line will make the cell "Read-Only". That is..
但是,第二行将使单元格“只读”。那是..
cell.userInteractionEnabled = false
Thanks
谢谢
Hope this helped.
希望这有帮助。