添加xib tableview单元的子视图(swift)

时间:2016-06-06 02:29:15

标签: ios swift uitableview

我创建了tableview和xib tableviewcell。我通过添加子视图来使用xib tableviewcell。但它似乎失去了func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

的功能

我希望此自定义tableviewcell继承cell中的正常tableview。 该怎么做??

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

    var myTableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)
    myTableView.dataSource = self
    myTableView.delegate = self

    myTableView.backgroundColor = UIColor.whiteColor()

    myTableView.frame = CGRectMake(20, 50, 250, 400)//Optional for table size

    myTableView.registerNib(UINib(nibName: "MyTableViewCell", bundle: nil), forCellReuseIdentifier: "myIdentifier")

    self.view.addSubview(myTableView)
}

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

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

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

    let myCell = tableView.dequeueReusableCellWithIdentifier("myIdentifier") as! MyTableViewCell
    myCell.myString1.text = "Row \(indexPath.row)"
    myCell.myString2.text = "String \(indexPath.row)"
    return myCell
}

}

1 个答案:

答案 0 :(得分:0)

您需要继承UITableViewController,而不是UIViewController。就这样:

{{1}}
相关问题