2017-06-01 21 views
0

我創建了一個UIViewController,並帶有一個容納UITableViewController的ContainerView。在ParentViewController的ViewDidLoad()方法中,我設置了兩個viewConroller之間的父子關係。嵌套在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上的gestureRecognizers,它們似乎是按順序的,你會期待考慮委託方法被觸發。

我也在模擬器和我的iPhone上運行這兩個,並觀察到兩者都有相同的行爲。

+0

原來這只是一些愚蠢的我忽略了,看到@phamot答案波紋管。隨意投票這個問題,因爲事實證明,與TVC在ContainerView或Swift 3中嵌套沒有任何關係 – TMin

回答

1

雨燕3.0:

必須設置小區選擇樣式

cell.selectionStyle = .default

+0

更好地仔細檢查:1)問題中發佈的代碼是Objective- C,不是Swift,2)我不認爲在Swift 3.0中這是真的。 – DonMag

+0

謝謝,該項目是Objective-C和Swift-3。 dataSource在Obj-c中。謝謝您的幫助。 – TMin

0

你必須做別的東西造成的行不突出。我只是試了一下,都是通過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