我创建了一个表视图和xib tableview单元格 . 我通过添加子视图使用xib tableview单元格 . 但是它似乎丢失了func tableView的功能(tableView:UITableView,didSelectRowAtIndexPath indexPath:NSIndexPath)
我希望这个自定义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 回答
您将要继承UITableViewController,而不是UIViewController . 因此: