2017-01-04 5 views
0

내 VC에는 UITableViewUIlabel이 있습니다. 내가 코드로 모든 것을했기 때문에 Storyboard에는 아무것도 없다.UITableView 대리인이 호출되지 않았습니다.

문제 :

  1. 나는 어떤 UITableViewDataSource and UITableViewDelegate가 호출되지 다음 UITable를 다시로드하지 않은 경우.

  2. 내가 아래 코드에서 수행 한 것처럼 UITable을 다시로드하면 cellForRow이 호출되지 않습니다.

나는 아래의 코드 작업을 수행했다.

import UIKit 


protocol DataVCProtocol 
{ 
    func addNewVC(Index : Int) 
} 


class DataVC: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    var delegate:DataVCProtocol? 

    var tblData : UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view. 

    } 

    override func viewDidAppear(_ animated: Bool) { 

     super.viewDidAppear(true) 

     self.view.backgroundColor = UIColor.clear 
     self.tblData = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width - 20 , height: UIScreen.main.bounds.size.height), style: UITableViewStyle.plain) 
     self.tblData.delegate = self 
     self.tblData.dataSource = self 
     self.tblData.register(TblDataCell.self, forCellReuseIdentifier: "dataCell") 

     self.tblData.showsHorizontalScrollIndicator = false 
     self.tblData.showsVerticalScrollIndicator = false 
     self.view.addSubview(self.tblData) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    // MARK:- TableView Datasource 
    // MARK:- 

    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{ 
     return 60 
    } 

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ 
     return 10 
    } 

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ 
     let dataCell : TblDataCell = tableView.dequeueReusableCell(withIdentifier: "dataCell") as! TblDataCell 

     dataCell.lblDataText.text = String("data cell #\(indexPath.row)") 

     return dataCell 
    } 

    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ 

     delegate?.addNewVC(Index: indexPath.row) 
    } 


} 

enter image description here

+10

'''tableView'''을 컨트롤러보기에 서브 뷰로 추가하는 것을 잊었습니다. – danypata

+0

"공개"키워드를 제거한 다음 시도하십시오. –

+0

@danypata 멋진 catch –

답변

5

이 부분을 놓친 그 방법 번 이상 그럴 필요가 있습니다 :

그래서 UITableView 또는 UICollectionView에 대한 대리자 메서드가 호출되지 않을 때마다 대개 3 가지 이유가 있습니다.

1. 가장 일반적으로 대리인이 설정되어 있지 않으므로이 경우에는 대리자가 호출되지 않습니다 (분명히).

2. numberOfRowsInSection이 0을 반환하거나 numberOfSections이 0을 반환하면이 메서드는 호출되지만 다른 메서드는 호출되지 않습니다.이 메서드는 문제를 디버깅하는 좋은 방법입니다.

3. tableView 또는 collectionView는 메인 컨트롤러에 서브 뷰로 추가되지 않습니다. 이것은 개발자가 스토리 보드 또는 xib 대신 코드를 선호 할 때 발생합니다. 이 경우 델리게이트가 설정되지만 (nil이 아님) 델리게이트의 메소드는 호출되지 않습니다. 또 다른 팁은 테이블 뷰 기본 디바이더가 표시되지 않습니다.

-2

당신이있는 tableview에 ALLOC 메모리에있는 셀을 마우스 오른쪽 버튼을 참조하십시오.

친절하게 알아보기.

var tblMenu: UITableView = UITableView() 

tblMenu.frame   = CGRectMake(0, 50, 320, 200);  tblMenu.delegate  = self 
tblMenu.dataSource = self 
tblMenu.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 

당신은 iOS에서 작동하고 내가 가진 정직 시작할 때해야합니다 내가 많은 DEVS 생각 문제에 대한 일반적인 대답을 게시 할 예정입니다

self.view.addSubview(tblMenu) 
+0

그는''viewDidLoad''' 메소드에서 테이블 뷰를 초기화하고 있습니다. – danypata

관련 문제