【问题标题】:UITableView nested in ContainerView not highlighting嵌套在 ContainerView 中的 UITableView 不突出显示
【发布时间】:2017-11-03 02:19:45
【问题描述】:

我创建了一个 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 上运行了这个,并且在两者上都观察到了相同的行为。

【问题讨论】:

  • 原来这只是我忽略的一些愚蠢的事情,请参阅@phamot 下面的答案。随意否决这个问题,因为它与将 TVC 嵌套在 ContainerView 或 swift 3 中没有任何关系

标签: ios uitableview swift3 uicontainerview


【解决方案1】:

您必须执行其他操作才能使行不突出显示。我只是试了一下,通过 Storyboard 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

【讨论】:

    【解决方案2】:

    斯威夫特 3.0:

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

    cell.selectionStyle = .default

    【讨论】:

    • 最好仔细检查一下... 1) 问题中发布的代码是 Objective-C,而不是 Swift,并且 2) 我认为无论如何在 Swift 3.0 中都不是这样。
    • 谢谢,这个项目是 Objective-C 和 Swift-3。数据源在 Obj-c 中。感谢您的帮助。
    猜你喜欢
    • 2019-05-18
    • 2018-04-10
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-10
    相关资源
    最近更新 更多