我想创建一个带有嵌入式 UITableView 的 UIViewController。我见过各种例子,但到目前为止还无法让它发挥作用。 socket 嵌入在表格单元格中。
class CREWStartMenuVCCell: UITableViewCell {
@IBOutlet weak var myButton: UIButton!
@IBOutlet weak var myTextView: UITextView!
}
class CREWStartMenuVC: UIViewController { // , UITableViewDelegate { // UITableViewDataSource,
@IBOutlet weak var tableView: UITableView! //<<-- TableView Outlet
override func viewDidLoad() {
super.viewDidLoad()
createAppData()
tableView.registerClass(CREWStartMenuVCCell.self, forCellReuseIdentifier: getCellName(viewName))
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> CREWStartMenuVCCell! {
let cell = tableView.dequeueReusableCellWithIdentifier(getCellName(viewName), forIndexPath: indexPath) as! CREWStartMenuVCCell
//处理不相关的单元格代码
如果我尝试覆盖以下函数,我会收到此错误:方法不会覆盖其父类(super class)中的任何方法。我错过了什么?
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { // was override
// calculate length of text and adjust the size of cell for that
return calculateTipsHeight(viewName, text:getTipsInfo(viewName, sectionNumber: indexPath.section, tipOrder: indexPath.row).tipItemText)
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {section))
return getTipsRowCount (viewName, sectionNumber: 0)
}
更新
当我尝试添加 UITableViewDataSource 时出现以下错误:
5:17: Protocol requires function 'tableView(_:cellForRowAtIndexPath:)' with type '(UITableView, cellForRowAtIndexPath: NSIndexPath) -> UITableViewCell'
34:10: Candidate has non-matching type '(UITableView!, cellForRowAtIndexPath: NSIndexPath!) -> CREWStartMenuVCCell!'
我没有看到错误。
最佳答案
由于类 CREWStartMenuVC 没有继承自 UITableViewController(或任何其他实现这些方法的类),因此可以(或必须)实现协议(protocol) UITableViewDelegate 和 UITableViewDataSource 中的方法,但不会覆盖它们。这意味着这些方法的实现之前不能有
override
此外,如果您没有在 Storyboard 中执行此操作,请不要忘记设置 tableView 的委托(delegate)和数据源,例如,在对 CREWStartMenuVC 类的 viewDidLoad() 的调用中。
tableView.delegate = self
tableView.dataSource = self
关于UPDATE的两个错误,直接替换
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> CREWStartMenuVCCell!
与
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> CREWStartMenuVCCell!
(唯一的区别是在类 UITableView 和 NSIndexPath 之后的感叹号!)
关于ios - 代码 : Unable to embed a UITableview in a UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33503610/