微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 未从自定义类调用cellForRowAtIndexPath

我正在使用 Xcode 7.0,Swift 2

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

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

请注意,如果我在ViewController.swift文件中使用相同的代码(即构建表所需的所有内容;不包括init函数等),它可以正常工作.

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

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,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”

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

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

解决方法

您的视图控制器没有对sideTest var的强引用.
一旦你的视图加载完毕,你的sideTest就是零.尽管你有一个tableview(通过添加子视图),但你不再拥有数据源.
func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {}

在视图加载后调用.那导致了问题.

将视图控制器更改为:

var tb :sideTest?


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

将您的cellforrowatindexpath更改为:

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

这将解决大多数问题.

原文地址:https://www.jb51.cc/iOS/331867.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐