Objective c 在不使用UITableViewController时,如何更改ClearSelectionOnInviewVillar外观?
我有一个UIViewController,它管理UISearchBar和UITableView。我读到苹果不鼓励让多个UIViewController管理应用程序的一部分,所以我没有使用UITableViewController来管理UITableView。相反,我在自己的UIViewController中实现了Objective c 在不使用UITableViewController时,如何更改ClearSelectionOnInviewVillar外观?,objective-c,ios,ipad,Objective C,Ios,Ipad,我有一个UIViewController,它管理UISearchBar和UITableView。我读到苹果不鼓励让多个UIViewController管理应用程序的一部分,所以我没有使用UITableViewController来管理UITableView。相反,我在自己的UIViewController中实现了UITableViewDelegate和UITableViewDataSource协议 我的问题是,既然我不再使用UITableViewController,我实际上如何更改ClearS
UITableViewDelegate
和UITableViewDataSource
协议
我的问题是,既然我不再使用UITableViewController,我实际上如何更改ClearSelectionInviewMillAppear
行为?此属性是UITableViewController的一部分。只需调用
[myTableView deselectRowAtIndexPath:[myTableView indexPathForSelectedRow] animated:YES];
在您的
视图中将出现:
方法。您很可能覆盖了视图将出现:动画
方法,而错过了[超级视图将出现:动画]
调用。这里是Swift代码:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if let indexPath = tableView.indexPathForSelectedRow() {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}
如果需要交互式动画,请执行以下操作:
- (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 5更新的UITableViewController内部发生的情况:
override func viewWillAppear(_ animated: Bool) {
if let indexPath = tableView.indexPathForSelectedRow {
tableView.deselectRow(at: indexPath, animated: true)
}
self.tableView.reloadData()
}
好的,我明白你的意思。所以本质上,我必须自己手动复制这个行为。这有点粗糙,特别是如果UITableViewController提供了其他更难实现的功能的话。我把它放在我的didSelectRowAtIndexPath中,它可以取消选择行<代码>tableView。取消RowAtIndexPath(indexPath,动画:true)您不需要重新加载整个tableView。
override func viewWillAppear(_ animated: Bool) {
if let indexPath = tableView.indexPathForSelectedRow {
tableView.deselectRow(at: indexPath, animated: true)
}
self.tableView.reloadData()
}