UITableView 嵌套在 ContainerView 中不突出显示
UITableView nested in ContainerView not highlighting
我创建了一个 UIViewController,其中包含一个包含 UITableViewController 的 ContainerView。在 ParentViewController 的 ViewDidLoad() 方法中,我设置了两个 viewConroller 之间的父子关系。
guard let childView = childViewController.view else {
return
}
addChildViewController(childViewController)
containerView.addSubview(childView)
... add constraints ...
childViewController.didMove(toParentViewController: self)
UITableViewController 呈现在 ContainerView 中。它正在正确滚动,但点击时单元格不会突出显示。具体来说,他们改变色调的动画没有发生。委托方法
-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
return true;
}
-(void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Oh no you didn't!");
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// stuff..
}
都在执行,但单元格的色调从未改变。
我查看了 UITableView 上的 gestureRecognizers,它们似乎是有序的,考虑到正在触发的委托方法,您会期望如此。
我也在模拟器和我的 iPhone 上 运行 这两个,并且在两者上观察到相同的行为。
Swift 3.0:
您必须设置单元格选择样式
cell.selectionStyle = .default
您一定是在做其他事情导致行不突出显示。我只是试了一下,通过 Storyboard UIContainerView
使用 "automating" 和手动加载并添加 "contained" 视图。也用 Swift 和 Objective-C UITableViewController
类.
按预期突出显示和取消突出显示的点击行。
//
// ManualContainerViewController.swift
//
import UIKit
class ManualContainerViewController: UIViewController {
@IBOutlet weak var containerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
if true {
//use swift table view controller
if let childViewController = storyboard?.instantiateViewController(withIdentifier: "containedTableVC") as? ContainedTableViewController {
guard let childView = childViewController.view else { return }
addChildViewController(childViewController)
containerView.addSubview(childView)
childView.translatesAutoresizingMaskIntoConstraints = false
childView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 0.0).isActive = true
childView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: 0.0).isActive = true
childView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 0.0).isActive = true
childView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: 0.0).isActive = true
childViewController.didMove(toParentViewController: self)
}
} else {
// use Objective-C table view controller
if let childViewController = storyboard?.instantiateViewController(withIdentifier: "oc_containedTableVC") as? OCContainedTableViewController {
guard let childView = childViewController.view else { return }
addChildViewController(childViewController)
containerView.addSubview(childView)
childView.translatesAutoresizingMaskIntoConstraints = false
childView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 0.0).isActive = true
childView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: 0.0).isActive = true
childView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 0.0).isActive = true
childView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: 0.0).isActive = true
childViewController.didMove(toParentViewController: self)
}
}
}
}
//
// ContainedTableViewController.swift
//
import UIKit
class ContainedTableViewController: UITableViewController {
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 30
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "basicCell", for: indexPath)
cell.textLabel?.text = "\(indexPath)"
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
}
//
// OCContainedTableViewController.h
//
#import <UIKit/UIKit.h>
@interface OCContainedTableViewController : UITableViewController
@end
//
// OCContainedTableViewController.m
//
#import "OCContainedTableViewController.h"
@interface OCContainedTableViewController ()
@end
@implementation OCContainedTableViewController
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 30;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"oc_basicCell" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"Section: %ld / Row: %ld", (long)indexPath.section, (long)indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
我创建了一个 UIViewController,其中包含一个包含 UITableViewController 的 ContainerView。在 ParentViewController 的 ViewDidLoad() 方法中,我设置了两个 viewConroller 之间的父子关系。
guard let childView = childViewController.view else {
return
}
addChildViewController(childViewController)
containerView.addSubview(childView)
... add constraints ...
childViewController.didMove(toParentViewController: self)
UITableViewController 呈现在 ContainerView 中。它正在正确滚动,但点击时单元格不会突出显示。具体来说,他们改变色调的动画没有发生。委托方法
-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
return true;
}
-(void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Oh no you didn't!");
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// stuff..
}
都在执行,但单元格的色调从未改变。
我查看了 UITableView 上的 gestureRecognizers,它们似乎是有序的,考虑到正在触发的委托方法,您会期望如此。
我也在模拟器和我的 iPhone 上 运行 这两个,并且在两者上观察到相同的行为。
Swift 3.0:
您必须设置单元格选择样式
cell.selectionStyle = .default
您一定是在做其他事情导致行不突出显示。我只是试了一下,通过 Storyboard UIContainerView
使用 "automating" 和手动加载并添加 "contained" 视图。也用 Swift 和 Objective-C UITableViewController
类.
按预期突出显示和取消突出显示的点击行。
//
// ManualContainerViewController.swift
//
import UIKit
class ManualContainerViewController: UIViewController {
@IBOutlet weak var containerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
if true {
//use swift table view controller
if let childViewController = storyboard?.instantiateViewController(withIdentifier: "containedTableVC") as? ContainedTableViewController {
guard let childView = childViewController.view else { return }
addChildViewController(childViewController)
containerView.addSubview(childView)
childView.translatesAutoresizingMaskIntoConstraints = false
childView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 0.0).isActive = true
childView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: 0.0).isActive = true
childView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 0.0).isActive = true
childView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: 0.0).isActive = true
childViewController.didMove(toParentViewController: self)
}
} else {
// use Objective-C table view controller
if let childViewController = storyboard?.instantiateViewController(withIdentifier: "oc_containedTableVC") as? OCContainedTableViewController {
guard let childView = childViewController.view else { return }
addChildViewController(childViewController)
containerView.addSubview(childView)
childView.translatesAutoresizingMaskIntoConstraints = false
childView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 0.0).isActive = true
childView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: 0.0).isActive = true
childView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 0.0).isActive = true
childView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: 0.0).isActive = true
childViewController.didMove(toParentViewController: self)
}
}
}
}
//
// ContainedTableViewController.swift
//
import UIKit
class ContainedTableViewController: UITableViewController {
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 30
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "basicCell", for: indexPath)
cell.textLabel?.text = "\(indexPath)"
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
}
//
// OCContainedTableViewController.h
//
#import <UIKit/UIKit.h>
@interface OCContainedTableViewController : UITableViewController
@end
//
// OCContainedTableViewController.m
//
#import "OCContainedTableViewController.h"
@interface OCContainedTableViewController ()
@end
@implementation OCContainedTableViewController
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 30;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"oc_basicCell" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"Section: %ld / Row: %ld", (long)indexPath.section, (long)indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end