我创建了一个tableview
和xib tableviewcell
。我使用xib tableviewcell
通过添加 subview 。但似乎失去了 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
}
}
最佳答案
您需要继承 UITableViewController,而不是 UIViewController。因此:
class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableview.delegate = self
self.tableview.backgroundColor = UIColor.whiteColor()
self.tableView.registerNib(UINib(nibName: "MyTableViewCell", bundle: nil), forCellReuseIdentifier: "myIdentifier")
)
关于ios - 添加 xib tableview 单元格的 subview (swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37648802/