ios - 代码 : Unable to embed a UITableview in a UIViewController

标签 ios uitableview uiviewcontroller swift2 xcode7

我想创建一个带有嵌入式 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/

相关文章:

ios - 在 UITableViewDelegate 方法 heightForRowAtIndexPath 中获取 TableView 单元格属性 :

objective-c - UITableViewCell textLabel,在使用 GCD 时直到滚动或触摸发生时才会更新

iphone - 如何正确更新表中部分中动态变化的行数(及其单元格内容)?

objective-c - 这种 View 样式可能吗?

iphone - 在 iOS 上,为什么 shouldAutorotateToInterfaceOrientation 被调用 10、12 或 13 次?

ios - 调用 FinalizeStatusForAccount 的结果在 swift 3 中未使用

ios - 如何在不使用 PresentViewController 的情况下以模态方式呈现自定义 View ?

iphone - 是否有适用于 iOS 的可自定义进度条?

iphone - 当前位置的注释

swift - 如何在 Swift 中为 UIViewController 子类创建自定义初始值设定项?