2017-12-26 13 views
2

내 코드 :cellForRowAt가 호출되고

//Adding Destination VC as subview in my current view's view container 
let storyboard = UIStoryboard(name: "Main", bundle: nil) 
     let viewController = storyboard.instantiateViewController(withIdentifier: "DestinationViewControllerID") 
     containerView.addSubview(viewController.view) 
     viewController.didMove(toParentViewController: self) 

//Now implementing Table view in the destination vc 

class DestinationViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

var tableView = UITableView() 
    var tableData = ["Beach", "Clubs", "Chill", "Dance"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     let screenBounds = UIScreen.main.bounds 
     let rect = CGRect(x: 0, y: 0, width: screenBounds.width, height: screenBounds.height) 
     tableView = UITableView(frame: rect, style: UITableViewStyle.plain) 
     tableView.dataSource = self 
     tableView.delegate = self 
     tableView.backgroundColor = UIColor.blue 

     tableView.register(UITableViewCell.self, forCellReuseIdentifier: "my") 

     view.addSubview(tableView) 

     tableView.reloadData() 
    } 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "my", for: indexPath) 
     cell.textLabel?.text = "This is row \(tableData[indexPath.row])" 

     return cell 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 4//tableData.count 
    } 
} 

그때 모든 것이 좋으을 작동하고 스토리 보드에서 내 초기 VC로 대상의 ViewController를 설정하고있어 경우에 경우에 하위 뷰를 같이 추가 container view cellForRowAt 메서드가 호출되지 않고 numberOfSection 및 numberOfRowsInSection이 호출됩니다.

해결책이있는 사람이 있습니까?

+3

당신은 당신의 UIViewController.View –

+0

신속한 버전은 무엇에 jQuery과를 추가해야합니까? – User511

+0

서브 뷰에 추가하기 전에 테이블 뷰를 새로 고침 – Jaydip

답변

3

 let storyboard = UIStoryboard(name: "Main", bundle: nil) 
    let viewController = storyboard.instantiateViewController(withIdentifier: "DestinationViewControllerID") 

    self.addChildViewController(viewController) 
    containerView.addSubview(viewController.view) 
    viewController.didMove(toParentViewController: self) 
0

제한 조건이 UITableView에 설정되어 있는지 확인하십시오. 그렇다면 UITableViewCell의 높이를 수동으로 설정하십시오.

+0

시도했지만 작동하지 않습니다. 답변 해 주셔서 감사합니다. –

+0

UITableView의 크기가 UITableViewCell의 높이가 아닌 데이터 소스 메서드에 영향을주지 않기 때문에 이것은 문제가되지 않습니다. –

0

다시 검사를 subviewing 전에 self.addChildViewController (의 ViewController를) 추가는 reusableidentifier

tableView.dequeueReusableCell (withIdentifier를 "내"에 대한 : indexPath) 등록 첫째

+0

이미 확인되었습니다. –

+0

이 줄을 제거하십시오. tableFor.register (UITableViewCell.self, forCellReuseIdentifier : "my") 및 try cell = UITableViewCell() –

+0

cellForRowAtIndex 자체가 호출되지 않고 셀을 등록하지 않아도 호출되어야하기 때문에 문제가 될 수 없습니다. –

0

, 스토리 보드에 tableview가 있어야합니다. 같이 함께 IBOutlet을

self.view.addSubview(tableView) 

또는 스토리 보드에있는 tableView를 추가하고 생성 :있는 tableView를 생성 한 후 다음을 수행하여 프로그래밍 방식으로 하위 뷰 수

@IBOutlet weak var tableView: UITableView! 

둘째 , 당신은 당신의 셀을 초기화하지 않은 당신을 이전에 생성 된 셀 (귀하의 경우에는 생성되지 않은 셀) 만 대기열에서 제외했습니다. 코드는 다음과 같이해야한다 :

var cell = tableView.dequeueReusableCell(withIdentifier: "my", for: indexPath) as UITableViewCell? 
if cell == nil { 
    cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "my") 
    } 
관련 문제