簡體   English   中英

不調用UITableView委托

[英]UITableView Delegate is not called

在我的VC中,有一個UITableView和一個UIlabel 因為我已經通過代碼完成了所有工作,所以Storyboard沒有任何內容。

問題 :

  1. 如果我沒有重新加載UITable則不會調用任何UITableViewDataSource and UITableViewDelegate

  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)
    }


}

看到正確的單元格

在此處輸入圖片說明

我將針對您的問題發布一個通用答案,我認為許多開發人員開始在iOS上工作時會遇到這些問題,老實說,我有很多次應該這樣做:

因此,只要不為UITableViewUICollectionView調用委托方法,通常會有3個主要原因。

1.最常見的是,未設置委托,因此它為nil,在這種情況下,顯然不會在委托上調用任何方法。

2. numberOfRowsInSection返回0或numberOfSections返回0,在這種情況下,將調用這些方法,但不會調用其他方法,這是調試問題的好方法。

3. tableView或collectionView不會作為子視圖添加到主控制器,這是在開發人員更喜歡代碼而不是情節提要或xibs時發生的。 在這種情況下,設置了委托(不是nil),但是沒有調用委托中的方法,對此的另一個提示是,您沒有看到表視圖默認的分隔符。

更新(致凱文評論)

4.檢查所有委托方法中返回的內容,因為某些返回值會導致其他方法被跳過

您必須分配內存給tableview。

請做下面的事情。

var tblMenu: UITableView  =   UITableView()

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

你錯過了這部分

self.view.addSubview(tblMenu)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM