【发布时间】:2011-02-08 03:36:51
【问题描述】:
我有一个要插入到 UITableView 顶部的单元格。如何确保当用户单击单元格时,它不会显示蓝色选定指示器?
【问题讨论】:
标签: iphone objective-c cocoa-touch uitableview
我有一个要插入到 UITableView 顶部的单元格。如何确保当用户单击单元格时,它不会显示蓝色选定指示器?
【问题讨论】:
标签: iphone objective-c cocoa-touch uitableview
要取消根据行索引选择任何表格单元格或特定表格单元格的功能,请使用willSelectRowAt
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
return nil
}
要简单地移除选择元素的 UI 效果,请将 selection style of the UITableViewCell 设置为 UITableViewCellSelectionStyleNone
斯威夫特 5:
selectionStyle = .none
【讨论】:
tableView:didSelectRowAtIndexPath: 在点击单元格时仍会被调用。要真正防止选择,您需要在 UITableViewDelegate 上 implement tableView:willSelectRowAtIndexPath: 并为不可选择的行返回 nil。
cell.selectionStyle = UITableViewCellSelectionStyle.none
cell.selectionStyle = .none
要在每个单元格的基础上使单元格完全不可选择,需要做两件事:
1- 正如其他人所说:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
2- 实现这个委托方法如下:
// Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.selectionStyle == UITableViewCellSelectionStyleNone){
return nil;
}
return indexPath;
}
【讨论】:
你可以的
cell.selectionStyle = UITableViewCellSelectionStyleNone;
【讨论】:
对于 Swift 3,您可以使用
cell.isUserInteractionEnabled = false
【讨论】:
tableView:didSelectRowAtIndexPath委托方法中,检测单元格索引,什么都不做,然后返回。
Swift 语法:
cell.selectionStyle = UITableViewCellSelectionStyle.None
【讨论】:
问题是如何使某些单元格可选择而其他单元格不可选择。这是我的解决方案,(在尝试了很多其他建议之后):
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
if (indexPath.row == 1 || indexPath.row == 2) {
return indexPath
}
return nil
}
【讨论】:
myTable.allowsSelection = false
【讨论】:
在 Swift 3 中更新:
cell.selectionStyle = UITableViewCellSelectionStyle.none
【讨论】:
对于 Swift 3:
cell.selectionStyle = .none
【讨论】:
实现这个method 的UITableViewDelegate
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
【讨论】:
你也可以试试这个
tableView.allowsSelection = false
【讨论】:
斯威夫特 5
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! UITableViewCell;
cell.selectionStyle = .none
}
【讨论】: