0

我正在尝试UITableView从 xib 文件加载一个。我创建了一个UIView用于加载 xib 文件的对象。在这里,我添加了一个,UITableView但是在 Xcode 的最后一次更新中,我无法弄清楚如何在这个表格视图中包含一个原型单元格。

这是我UIView加载xib文件的代码:

class TopChartView: UIView, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet var tableViewChart: UITableView!

    var view : UIView!    

    var prova = [String]()


    override init(frame: CGRect) {
        super.init(frame: frame)


        prova.append("hi")
        prova.append("bye")

        self.tableViewChart = UITableView()

        self.tableViewChart.delegate = self
        self.tableViewChart.dataSource = self

        self.tableViewChart.registerClass(TopChartTVCell.self, forCellReuseIdentifier: "TopChart_IDcell")

        //carico il file xib
        xibSetup()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    func xibSetup() {

        Global.log.action()

        view = loadViewFromNib()
        // Make the view stretch with containing view
        view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
        self.addSubview(view)

        Global.log.action(methodEnded: true)
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        Global.log.action()

        let screenSize: CGRect = UIScreen.mainScreen().bounds
        view.frame.size.width = screenSize.width
        view.frame.size.height = screenSize.height
        view.frame.origin.x = 0
        view.frame.origin.y = 0

        Global.log.action(methodEnded: true)
    }

    func loadViewFromNib() -> UIView {

        Global.log.action()

        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: Constants.topChartVC.xibFileName, bundle: bundle)
        let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView

        Global.log.action(methodEnded: true)
        return view
    }



    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return self.prova.count
    }

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

        let cell = tableView.dequeueReusableCellWithIdentifier("TopChart_IDcell", forIndexPath: indexPath) as! TopChartTVCell

        let elem = self.prova[indexPath.row]

        cell.label.text = elem
        return cell
    }
}

这里有我的简单 xib 这是问题,因为我无法在UITableViewCell里面添加UITableView

在此处输入图像描述

4

0 回答 0