【问题标题】:Separating ViewController and UITableViewDataSource分离 ViewController 和 UITableViewDataSource
【发布时间】:2015-08-07 13:42:29
【问题描述】:

我正在尝试通过将 UITableViewDataSource(和 Delegate)移动到单独的类来清理拥挤的 ViewController。

虽然 DataSource 在 ViewController 中(见下文),但它运行良好

class ViewController: UIViewController, UITableViewDataSource{

//MARK: Properties
@IBOutlet weak var myTableView: UITableView!
let cellIdentifier = "myTableViewCell"
let myArray = ["Label one", "Label two", "Label three"]


override func viewDidLoad()
{
    super.viewDidLoad()

    myTableView.dataSource = self
}

override func didReceiveMemoryWarning()
    {super.didReceiveMemoryWarning()}


//MARK: TableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int
    {return 1}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {return myArray.count}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! myTableViewCell

    cell.myLabel.text = myArray[indexPath.row]

    return cell
}
}

使用 myTableViewCell 类

class myTableViewCell: UITableViewCell{

@IBOutlet weak var myLabel: UILabel!

override func awakeFromNib()
    {super.awakeFromNib()}

override func setSelected(selected: Bool, animated: Bool)
    {super.setSelected(selected, animated: animated)}
}

这工作正常,并使用从 myArray 中的字符串填充的标签填充基本表。

但是,当我将 DataSource 移动到它自己的类时,如下所示,它不起作用

视图控制器

class ViewController: UIViewController
{
//MARK: Properties
@IBOutlet weak var myTableView: UITableView!


override func viewDidLoad()
{
    super.viewDidLoad()

    myTableView.dataSource = myDataSource()
}

override func didReceiveMemoryWarning()
{super.didReceiveMemoryWarning()}

}

这是 myDataSource 类

class myDataSource: NSObject, UITableViewDataSource
{
let cellIdentifier = "myTableViewCell"
let myArray = ["Label one", "Label two", "Label three"]

//MARK: TableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{return 1}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{return myArray.count}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! myTableViewCell

    cell.myLabel.text = myArray[indexPath.row]

    return cell
}
}

(myTableViewCell 保持不变)

此设置仅输出一个空表,尽管我所做的只是将代码从 ViewController 复制粘贴到 myDataSource。我错过了什么? (除了委托。我稍后会处理,首先我需要找出数据源的问题)。 我是一个快速入门的新手,所以我很难理解我在哪里出错了。任何帮助都将不胜感激。如果你能在你的回答中记住我刚刚开始,所以尽量不要在没有解释的情况下向我抛出太多复杂的概念。谢谢

【问题讨论】:

    标签: ios swift uitableview xcode6 swift2


    【解决方案1】:

    可能您的数据源中的代码永远不会被调用,因为您的数据源不会被此处的表保留myTableView.dataSource = myDataSource(),因此基本上会立即发布。要解决此问题,请将数据源保留为属性。

    【讨论】:

    • 谢谢!这解决了问题:)
    【解决方案2】:

    将单独的类声明为ViewController 类的扩展

    extension ViewController: UITableViewDataSource
    {
        let cellIdentifier = "myTableViewCell"
        let myArray = ["Label one", "Label two", "Label three"]
    
        //MARK: TableViewDataSource
        func numberOfSectionsInTableView(tableView: UITableView) -> Int
        {return 1}
    
        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
        {return myArray.count}
    
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
        {
            let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! myTableViewCell
            cell.myLabel.text = myArray[indexPath.row]
            return cell
        }
    }
    

    我也总是在扩展声明中使用委托方法,但在与主类相同的文件中

    【讨论】:

      猜你喜欢
      • 2016-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多