嵌套在ContainerView中的UITableView没有突出显示

时间:2017-06-01 19:46:09

标签: ios uitableview swift3 uicontainerview

我创建了一个带有ContainerView的UIViewController,它包含一个UITableViewController。在ParentViewController的ViewDidLoad()方法中,我设置了两个viewConrollers之间的父子关系。

 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上运行它,并且在两者上都观察到了相同的行为。

2 个答案:

答案 0 :(得分:1)

Swift 3.0:

您必须设置单元格选择样式

cell.selectionStyle = .default

答案 1 :(得分:0)

您必须做其他事情才能使行不突出显示。我只是尝试了一下,使用"自动化"它通过故事板UIContainerView并通过手动加载和添加"包含"视图。同时使用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