ios 返回时取消选择表视图行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3977406/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:57:09  来源:igfitidea点击:

Deselecting the table view row when returning

iphoneobjective-cuitableviewios

提问by cannyboy

I followed Luke Redpath's suggestion here - Selected UItableViewCell staying blue when selected- to deselect the row when returning to the original table view, but I can't get it working. In fact the method doesn't get triggered.

我在这里遵循了 Luke Redpath 的建议 - Selected UItableViewCell 在选择时保持蓝色- 在返回原始表格视图时取消选择该行,但我无法让它工作。事实上,该方法不会被触发。

- (void)viewDidAppear:(BOOL)animated
{
  [super viewDidAppear:animated];
  [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

I suspect this is because the class isn't a UITableViewController, but a UIViewControllerwith the tableViewas a property connected up to the NIB.

我怀疑这是因为该类不是一个UITableViewController,而是一个UIViewControllertableView作为属性连接到NIB。

How would I get the same behavior - ie deselecting when returning?

我将如何获得相同的行为 - 即返回时取消选择?

回答by ahmad

why you just don't deselect it in the delegate method

为什么你只是不在委托方法中取消选择它

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];

}

回答by grilix

If you are using a NavigationController, you can do it with its delegate:

如果您使用的是 NavigationController,则可以使用其委托来完成:

- (void)navigationController: (UINavigationController *)navigationController
       didShowViewController: (UIViewController *)viewController
                    animated: (BOOL)animated
{
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

But you have to check whether the show controller is which you want =/

但是,你必须检查显示控制器是否是你想要= /

[EDIT] Create the outlet and link it in IB to NavigationController

[编辑] 创建插座并将其在 IB 中链接到 NavigationController

@interface MyInt <UINavigationControllerDelegate>
{
    IBOutlet UINavigationController *navigation;
    ...
}
@property (nonatomic, retain) UINavigationController *navigation;
...
@end

@implementation MyInt
@syntesize navigation;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigation setDelegate: self];
}
...

@end

回答by Alexander Wallin

Are you selecting the row programmatically? If so, I had the same problem when selecting a row using [cellToBeSelected setSelected:YES];.

您是否以编程方式选择行?如果是这样,我在使用[cellToBeSelected setSelected:YES];.

By using the UITableViewController's selectRowAtIndexPath:animated:scrollPosition:method it kept track of the current selected indexPath.

通过使用UITableViewControllerselectRowAtIndexPath:animated:scrollPosition:方法,它跟踪当前选定的 indexPath。

In fact, if clearsSelectionOnViewWillAppearis set to true, one does not have deselect rows manually.

实际上,如果clearsSelectionOnViewWillAppear设置为true,则无需手动取消选择行。

回答by FrankWest

I hade the same problem, but after debugging I found out the indexPathForSelectedRow count was 0 so I double checked my code and noticed I was calling

我遇到了同样的问题,但在调试后我发现 indexPathForSelectedRow 计数为 0,所以我仔细检查了我的代码并注意到我正在调用

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // some code...
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

After deleting this line it just worked as expected.

删除此行后,它按预期工作。

回答by Rajesh Loganathan

Very simple :Try this line.

很简单:试试这条线。

Add deselectRowAtIndexPathin your didSelectRowAtIndexPath

加入deselectRowAtIndexPath你的didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

回答by Dhruv Khatri

Swift 3.0

斯威夫特 3.0

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            tableView.deselectRow(at: indexPath, animated: true)
    //Write Your Other code Here.
            }

It Will deselect Row after you click on It.

单击它后,它将取消选择行。

回答by Ranjeet Singh

-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

-(void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:[UIColor grayColor]];
}

-(void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:[UIColor clearColor]];
}