不调用UITableView Delegate

问题描述 投票:0回答:2

在我的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)
    }


}

看到正确的细胞

enter image description here

ios swift uitableview
2个回答
6
投票

我将为你的问题发布一个通用的答案,我认为当他们开始在iOS上工作时会有很多开发人员,并且说实话,我应该有更多次的方式:

因此,无论何时没有为UITableViewUICollectionView调用委托方法,通常有3个主要原因。

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

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

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

更新(谢谢Kevin评论)

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


-2
投票

你必须将内存分配给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)
© www.soinside.com 2019 - 2024. All rights reserved.