Ответ 1
Просто позвонив
[myTableView deselectRowAtIndexPath:[myTableView indexPathForSelectedRow] animated:YES];
в вашем методе viewWillAppear:
.
У меня есть UIViewController, который управляет UISearchBar и UITableView. Я читал, что Apple препятствует тому, что несколько UIViewControllers управляют частью вашего приложения, поэтому я не использовал UITableViewController для управления UITableView. Вместо этого я реализовал протокол UITableViewDelegate
и UITableViewDataSource
в своем собственном UIViewController.
Мой вопрос в том, что я больше не использую UITableViewController, как мне изменить поведение clearsSelectionOnViewWillAppear
? Это свойство является частью UITableViewController.
Просто позвонив
[myTableView deselectRowAtIndexPath:[myTableView indexPathForSelectedRow] animated:YES];
в вашем методе viewWillAppear:
.
Здесь код Swift:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if let indexPath = tableView.indexPathForSelectedRow() {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}
Вы, скорее всего, переопределяете метод viewWillAppear:animated
и не получаете вызов [super viewWillAppear:animated]
.
Если вы хотите интерактивную анимацию:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
if(selectedIndexPath) {
if(self.transitionCoordinator != nil) {
[self.transitionCoordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
[self.tableView deselectRowAtIndexPath:selectedIndexPath animated:YES];
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
}];
[self.transitionCoordinator notifyWhenInteractionChangesUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext> context) {
if(context.cancelled) {
[self.tableView selectRowAtIndexPath:selectedIndexPath
animated:YES
scrollPosition:UITableViewScrollPositionNone];
}
}];
} else {
[self.tableView deselectRowAtIndexPath:selectedIndexPath animated:animated];
}
}
}
Swift Version:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let selectedIndexPath = self.tableView.indexPathForSelectedRow {
if let transitionCoordinator = self.transitionCoordinator {
transitionCoordinator.animate(alongsideTransition: { (context) in
self.tableView.deselectRow(at: selectedIndexPath, animated: true)
}, completion: nil)
transitionCoordinator.notifyWhenInteractionChanges { (context) in
if context.isCancelled {
self.tableView.selectRow(at: selectedIndexPath, animated: true, scrollPosition: .none)
}
}
} else {
self.tableView.deselectRow(at: selectedIndexPath, animated: animated)
}
}
}
Теперь вы можете очистить анимацию, т.е. при интерактивном извлечении из контроллера навигации. Он также повторно выбирает строку, если взаимодействие было отменено. Это ближе к тому, что происходит внутри UITableViewController
.
Обновлено для Swift 5:
override func viewWillAppear(_ animated: Bool) {
if let indexPath = tableView.indexPathForSelectedRow {
tableView.deselectRow(at: indexPath, animated: true)
}
self.tableView.reloadData()
}