未从自定义类调用cellForRowAtIndexPath [英] cellForRowAtIndexPath is not being called from custom class

查看:96
本文介绍了未从自定义类调用cellForRowAtIndexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xcode 7.0,Swift 2

I'm using Xcode 7.0, Swift 2

我基本上是在尝试创建一个自定义类来构建 UITable ,然后在 ViewController 中创建表的新对象并将其加载到self.view中;

I'm basically trying to create a custom class that will build a UITable, then in the ViewController I make a new object of the table and load it into self.view;

我遇到的问题是函数 func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath) - > UITableViewCell 根本没有在自定义类中调用。我一直在寻找一个解决方案3天,我已经尝试重建应用程序和代码几次没有运气。

The problem I'm having is that the function func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell isn't being called at all from within the custom class. I've been looking for a solution for 3 days now and I've tried rebuilding the App and code several times with no luck.

请注意,如果我使用在ViewController.swift文件中,相同的代码(即构建表所需的一切;排除init函数等),它工作正常。

Please note, if I use the same code (that is everything required to build the table; excluding init functions, etc) in the ViewController.swift file, it works fine.

我知道问题出在 cellForRowAtIndexPath 函数,因为它在运行时不会打印出我在该代码块中设置的语句。调用所有其他函数,但由于某种原因,这不会被调用。不确定我是否在这里忽略了一些东西。任何帮助,将不胜感激。提前致谢。

I know the problem is with the cellForRowAtIndexPath function because it will not print out the statement I set in that block of code when it runs. All other functions are called, but for some reason this isn't being called. Not sure if I overlooked something here. Any help would be appreciated. Thanks in advance.

class sideTest: NSObject, UITableViewDelegate, UITableViewDataSource {


    let tesTable: UITableView = UITableView()
    var items: [String]?
    var mView: UIView = UIView()

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print("The number of rows is: \(self.items!.count)")

        return self.items!.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        print("\nLets create some cells.")

        let sCell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell!

        sCell.textLabel?.text = self.items![indexPath.row]
        sCell.textLabel?.textColor = UIColor.darkTextColor()

        return sCell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("You selected cell #\(indexPath.row)!")
    }

    func tblSetup() {

        self.tesTable.frame = CGRectMake(0, 0, 320, mView.bounds.height)
        self.tesTable.delegate = self
        self.tesTable.dataSource = self

        self.tesTable.backgroundColor = UIColor.cyanColor()

        // load cells
        self.tesTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        self.tesTable.reloadData()

        print("Currenlty in tblSetup.\nCurrent rows is: \(self.items!.count)")
    }

    //Init

    override init() {
        super.init()

        self.items = nil

        self.tblSetup()
    }

    init(sourceView: UIView , itemListAsArrayString: [String]) {
        super.init()

        self.items = itemListAsArrayString
        self.mView = sourceView

        self.tblSetup()
    }
}

以下是ViewController.swift的代码;请注意表格已构建,但单元格不会填充,即使我手动输入单元格信息: sCell.textLabel?.text =test cell

Here is the code from ViewController.swift; Please do note that the table gets built, but the cells do not populate, even if I manually enter cell info by doing: sCell.textLabel?.text = "test cell"

class ViewController: UIViewController {



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

        let myTable: sideTest = sideTest(sourceView: self.view, itemListAsArrayString: ["Cell 1", "Cell 2", "Cell 3"])
        self.view.addSubview(myTable.tesTable)
    }

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

再次,非常感谢任何帮助。谢谢。

Again, any help is greatly appreciated. Thanks.

推荐答案

您的视图控制器没有对sideTest变量的强引用。
一旦你的视图加载完毕,你的sideTest就是零。虽然你有一个tableview(通过添加子视图),但你不再有数据源。

Your view controller don't have a strong reference to your sideTest var. Once your view did load finished,your sideTest is nil.Although you have a tableview(by add subview), but you no longer have a data source.

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {}

在视图加载后调用。这会导致问题。

is called after view did load. That cause the problem.

将您的视图控制器更改为:

change your view controller to:

    var tb :sideTest?


override func viewDidLoad() {
    super.viewDidLoad()
    let myTable: sideTest = sideTest(sourceView: self.view, itemListAsArrayString: ["Cell 1", "Cell 2", "Cell 3"])
    print(myTable.tesTable.frame)
    tb=myTable
    self.view.addSubview(myTable.tesTable)
}

将您的cellforrowatindexpath更改为:

change your cellforrowatindexpath to:

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    print("create cells")
    var cell :UITableViewCell?

    if let sCell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell"){
       cell=sCell

    }else{
        cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")

    }


    cell!.textLabel?.text = self.items![indexPath.row]
    cell!.textLabel?.textColor = UIColor.darkTextColor()

    return cell!
}

这将解决大部分问题。

这篇关于未从自定义类调用cellForRowAtIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆