返回时取消选择表视图行

can*_*boy 32 iphone objective-c uitableview ios

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

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

我怀疑这是因为该类不是a UITableViewController,而是作为UIViewControllertableViewNIB连接的属性.

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

ahm*_*mad 21

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

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

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

}
Run Code Online (Sandbox Code Playgroud)

  • 你不必做`[self.tableView indexPathForSelectedRow]`,indexPath已经是一个参数,你可以在那里插入它. (2认同)

gri*_*lix 16

如果您使用的是NavigationController,则可以使用其委托执行此操作:

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

但你必须检查节目控制器是否是你想要的= /

[编辑]创建插座并将其链接到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
Run Code Online (Sandbox Code Playgroud)


Ale*_*lin 8

您是否以编程方式选择行?如果是这样,我在选择行时遇到了同样的问题[cellToBeSelected setSelected:YES];.

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

实际上,如果clearsSelectionOnViewWillAppear设置为true,则表示没有手动取消选择行.


小智 8

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

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

    // some code...
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

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