UITableView Delegate未调用

时间:2017-01-04 10:44:18

标签: ios swift uitableview

在我的VC中,有一个UITableView和一个UIlabel。正如我通过代码完成所有事情,Storyboard中没有任何内容。

问题:

  1. 如果我没有重新加载UITable,则不会调用任何UITableViewDataSource and UITableViewDelegate

  2. 如果我像我在下面的代码中那样重新加载UITable,则不会调用cellForRow

  3. 我已完成以下代码工作。

        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

2 个答案:

答案 0 :(得分:6)

我会针对您的问题发布一个通用的答案,我认为很多开发人员在开始使用iOS时会有这些答案,而且说实话,我有更多时间应该拥有它:

因此,只要没有为template<typename T> using Callback = std::function<void (qint64,T,CallbackError)>; UITableView调用委托方法,通常会有3个主要原因。

1.最常见的是,代理没有设置,所以它没有,在这种情况下,代理人不会调用任何方法(显然)。

2. UICollectionView返回0或numberOfRowsInSection返回0,在这种情况下将调用这些方法,但不会调用其他方法,这是调试问题的好方法。

3. tableView或collectionView没有作为子视图添加到主控制器,当开发人员更喜欢代码而不是故事板或xib时,就会发生这种情况。在这种情况下,委托被设置(不是nil)但是没有来自委托的方法被调用,另一个提示是,你没有看到表视图的默认分隔符。

更新(请注意凯文评论)

4.检查您在所有委托方法中返回的内容,因为某些返回值会导致跳过其他方法

答案 1 :(得分:-2)

你必须将内存分配给tableview。

请做下面的事情。

height:inherit

你错过了这部分

var tblMenu: UITableView  =   UITableView()

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