Ios 为什么UITableViewCell保持高亮显示?
什么会导致表格视图单元格在被触摸后保持高亮显示?我单击该单元,可以看到它在推送局部视图时保持高亮显示。弹出详细视图后,单元格仍会高亮显示。在您的Ios 为什么UITableViewCell保持高亮显示?,ios,uitableview,cocoa-touch,Ios,Uitableview,Cocoa Touch,什么会导致表格视图单元格在被触摸后保持高亮显示?我单击该单元,可以看到它在推送局部视图时保持高亮显示。弹出详细视图后,单元格仍会高亮显示。在您的didSelectRowAtIndexPath中,您需要调用取消选择ROWATINDEXPATH以取消选择单元格 因此,无论您在didSelectRowAtIndexPath中执行什么操作,您只需让它调用diselectRowatindexpath - (void)tableView:(UITableView *)tableView didSelectR
didSelectRowAtIndexPath
中,您需要调用取消选择ROWATINDEXPATH
以取消选择单元格
因此,无论您在didSelectRowAtIndexPath
中执行什么操作,您只需让它调用diselectRowatindexpath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Do some stuff when the row is selected
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
您是否将子类
-(void)viewwillbeen:(BOOL)animated
?当您不调用[超级视图将显示:动画]时,选定的UITableViewCell不会取消选择代码>在您的自定义方法中。我的深入应用程序也遇到了这个问题。当一个viewcontroller(我称之为VC)在按下另一个viewcontroller后返回后,VC中选定的单元格保持高亮显示。在我的应用程序中,我创建了VC来处理第二个级别(三个级别中的第二个)
在我的例子中,问题是VC是一个UIViewController(它包含一个包含TableView的视图)。我改为让VC成为UITableViewController(包含TableView)。UITableViewController类在推送返回后自动处理表格单元格的取消高亮显示。“帖子”的第二个答案对这个问题给出了更完整的答案
根viewcontroller没有出现问题,因为当我在XCode中将应用程序创建为“基于导航的应用程序”时,生成的根viewcontroller已生成UITableViewController子类。要获得Kendall Helmstetter Gelner在其评论中描述的行为,您可能不希望取消RowAtIndexPath,而是希望控制器上的ClearSelectionInviewMillAppear属性。也许这是偶然设置为“是”的
有关新的UITableViewController子类,请参见默认Apple模板中的注释:
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
}
如果您使用的是UITableViewCell,请对以下行进行注释
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
// [super setSelected:selected animated:animated];
}
希望这有帮助。最干净的方法是在视图上显示:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// Unselect the selected row if any
NSIndexPath* selection = [self.tableView indexPathForSelectedRow];
if (selection) {
[self.tableView deselectRowAtIndexPath:selection animated:YES];
}
}
这样,当您返回到控制器时,您就有了淡出选择的动画,这是应该的
取自
Swift版本
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// deselect the selected row if any
let selectedRow: IndexPath? = tableView.indexPathForSelectedRow
if let selectedRowNotNill = selectedRow {
tableView.deselectRow(at: selectedRowNotNill, animated: true)
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
}
我已经有相同的问题很长一段时间了,所以如果其他人正在挣扎:
查看您的-tableView:cellforrowatinexpath:
,看看您是在创建单元格还是在使用“重用标识符”。如果是后者,请确保IB中的表具有具有该标识符的单元格。如果不使用重用标识符,只需为每行创建一个新单元格
然后,这将在出现时为您的表提供预期的“淡入选定行”。在UITableViewCell类中使用此方法
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
// Just comment This line of code
// [super setSelected:selected animated:animated];
}
对于Swift用户,请将以下内容添加到您的代码中:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
这是保尔斯泰德的回答,除了SWIFT,而不是Obj-C./P> < P>如果这些都不为你工作,请考虑这项工作:
使用展开序列调用:
@IBAction func unwind_ToTableVC (segue: UIStoryboardSegue) {
if let index = tableView.indexPathForSelectedRow {
tableView.deselectRowAtIndexPath(index, animated: true)
}
}
为什么要这样做?主要是当您无法在正确的时间运行取消选择代码时。我有麻烦,它没有工作的视图将出现,所以放松工作了很多
步骤:
将展开顺序(或从上面粘贴)写入第一个VC(带表格的VC)
去第二个VC。控制从Cancel/Done/Etc按钮的拖动,您使用该按钮关闭VC并拖动到顶部的退出图标
选择在步骤1中创建的展开段
祝你好运
我使用的是CoreData,因此对我有效的代码是来自各种答案的想法的组合,在Swift中:
override func viewDidAppear(_ animated: Bool) {
if let testSelected = yourTable.indexPathForSelectedRow {
yourTable.deselectRow(at: testSelected, animated: true)
}
super.viewDidAppear(true)
}
对于Swift 3:
我希望它能用在视窗里
定义:-
var selectedIndexPath = IndexPath()
在视图中:-
override func viewDidDisappear(_ animated: Bool) {
yourTableView.deselectRow(at: selectedIndexPath, animated: true)
}
在didSelectRowAtIndexPath中:-
func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
selectedIndexPath = indexPath
}
Swift 3解决方案
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// deselect the selected row if any
let selectedRow: IndexPath? = tableView.indexPathForSelectedRow
if let selectedRowNotNill = selectedRow {
tableView.deselectRow(at: selectedRowNotNill, animated: true)
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
}
使用Swift 4更新
经过几次实验,也基于之前的答案,我得出结论,最佳行为可以通过两种方式实现:(在实践中几乎相同)
我个人采用第一种解决方案,因为它更简洁。如果返回到tableView时需要一些动画,另一种可能性是使用viewwillbeen
:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let selectedRow: IndexPath? = _view.tableView.indexPathForSelectedRow
if let selectedRow = selectedRow {
_view.tableView.deselectRow(at: selectedRow, animated: true)
}
}
最后但并非最不重要的一点是,如果您使用的是UITableViewController,还可以利用该属性 如果该单元格在触摸后仍保持高亮显示,则可以调用UITabelView方法
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nsindepath*)indepath
{
`[tableView deselectRowAtIndexPath:indexPath animated:YES];`
}
或者,您可以使用以下方法并根据您的要求进行修改
//标记:UITableViewDelegate
func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
cell.backgroundColor = UIColor.greenColor()
}
}
func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
cell.backgroundColor = UIColor.blackColor()
}
}
代码10,Swift 4
我也遇到了同样的问题,发现我在tableViewController的底部留下了一个对ViewWill的空调用。删除空覆盖函数后,返回tableView视图时,该行不再保持高亮显示
问题函数
override func viewWillAppear(_ animated: Bool) {
// need to remove this function if not being used.
}
删除空函数解决了我的问题 Swift 5解决方案:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
}
我更喜欢在我的视图中调用取消行索引路径
,如果选择行会显示一个新视图。实际上,这不是调用它的正确位置,真的。。。尝试使用带有表格的Apple应用程序(Contacts是一个不错的应用程序),你会看到,当你推到一个新屏幕后,返回时,手机在被取消选择之前仍然会短暂高亮显示。从理论上讲,我认为您不必做任何额外的工作就可以立即取消选择它,因此不需要ViewDidAppeal中的代码…@Kendall,@4thSpace:也许我最后的评论混淆了我指的是谁,对此表示歉意。UITableViewController调用其tableView属性上的-viewdiDisplay
中的-descroatingIndexPath:animated:
方法。然而,如果你有