2017-06-01 16 views
0

私は、UITableViewControllerを保持するContainerViewでUIViewControllerを作成しました。 ParentViewControllerのViewDidLoad()メソッドでは、2つのviewConrollersの間に親子関係を設定します。ContainerViewにネストされたUITableViewがハイライト表示されない

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のジェスチャーリコグニザーを見てきました。そして、それらは順番になっているように見えます。これはデリゲートメソッドがトリガーされていると考えられます。

私はこれもシミュレータと私のiPhoneの両方で実行しており、両方で同じ動作を観察しています。

+0

それは私が見落として愚かなだけで何かだったが判明し、@phamotによって回答怒鳴るを参照してください。コンシューマビューでTVCを入れ子にしたり、すぐに3をスローしたりとは関係ないことが判明したので、この質問に投票してください。 – TMin

答えて

1

スウィフト3.0:

あなたは、あなたが強調表示しない行を引き起こすために何かをやっている必要がありますセル選択スタイルに

cell.selectionStyle = .default

+0

よくチェックしてください... 1)問題に投稿されたコードはObjective- C、スウィフトではない、そして2)私はスウィフト3.0でそれが真実だとは思わない。 – DonMag

+0

おかげさまで、このプロジェクトは客観的なCとSwift-3の両方です。 dataSourceはObj-cにあります。助けてくれてありがとう。 – TMin

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 
関連する問題