【问题标题】:Adding a table view to a UI View Controller error将表格视图添加到 UI 视图控制器错误
【发布时间】:2015-06-29 10:04:12
【问题描述】:

我有一个带有视图控制器的情节提要。视图控制器分为两半,一半是 UI 日期选择器,另一半是表格视图。当我将视图控制器设置为委托和数据源时,出现以下错误

reason: '-[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7fdb29b4d9e0'

我的View控制器是这样定义的

import Foundation
import UIKit

class CustomViewController:  UIViewController , UITableViewDelegate , UITableViewDataSource{

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

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


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

        return 3;
    }


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

        var sampleCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell

        return sampleCell;

    }



}

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    你确定你的视图的类是你的表的类吗?当我忘记设置视图的类时,我遇到了同样的错误。

    【讨论】:

    • 很高兴我帮了忙 :) 谢谢现在我可以到处评论了哈哈 :)
    【解决方案2】:

    你有连接到故事板/笔尖中的 UITableView 的 IBOutlet 属性吗?视图控制器是否设置为表视图的委托和数据源?

    【讨论】:

    • 没有在 storyboard 中定义 IBOutlets。视图控制器设置为数据源和委托,当我将整个视图控制器用作情节提要时,它工作正常,当我像上面提到的那样划分视图控制器时它会失败
    【解决方案3】:

    你需要为 UITableView 设置一个 outlet 并设置代理:

    然后在 viewDidLoad 中设置 tableView 的数据源和委托给 self,因为 ViewController 实现了协议。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-03
      • 1970-01-01
      • 1970-01-01
      • 2021-02-27
      • 2023-03-11
      • 1970-01-01
      • 2013-04-26
      • 2013-06-05
      相关资源
      最近更新 更多