简体   繁体   English

从UITableViewController更改为UITableView时发生错误

[英]Errors when changing from UITableViewController to UITableView

I'm trying to embed a Table View in a regular View Controller because I want some other Views in the View Controller besides just the Table View. 我试图在常规视图控制器中嵌入表视图,因为除了表视图之外,我还希望在视图控制器中添加其他视图。 I had a Table View Controller with some methods that I was overriding. 我有一个表视图控制器,其中包含一些我要重写的方法。 I copied the code for the Table View Controller, pasted it into a Table View .swift file, and removed the "override" before each method as the methods weren't inherent to the Table View class. 我复制了Table View Controller的代码,将其粘贴到Table View .swift文件中,并删除了每个方法之前的“替代”,因为这些方法不是Table View类固有的。 Unfortunately I'm still getting errors in a few lines of the code: 不幸的是,我在几行代码中仍然遇到错误:

  1. super.viewDidLoad() "Value of type 'UITableView' has no member 'viewDidLoad'" “ super.viewDidLoad()” UITableView“类型的值没有成员'viewDidLoad'”

  2. tableView.rowHeight = UITableViewAutomaticDimension "Ambiguous reference to member 'tableView(_:numberOfRowsInSection:)'" “ tableView.rowHeight = UITableViewAutomaticDimension”对成员'tableView(_:numberOfRowsInSection :)的模糊引用”

  3. tableView.estimatedRowHeight = 60.0 "Ambiguous reference to member 'tableView(_:numberOfRowsInSection:)'" tableView.estimatedRowHeight = 60.0“对成员'tableView(_:numberOfRowsInSection :)'的引用不明确”

  4. super.didReceiveMemoryWarning() "Value of type 'UITableView' has no member 'didReceiveMemoryWarning'" “ super.didReceiveMemoryWarning()“类型'UITableView'的值没有成员'didReceiveMemoryWarning'”

  5. tableView.insertRows(at: [indexPath], with: .automatic) "Ambiguous reference to member 'tableView(_:numberOfRowsInSection:)'" “ tableView.insertRows(at:[indexPath],带有:.automatic)”对成员'tableView(_:numberOfRowsInSection :)的不明确引用”

What do I need to do to those lines of code to make my program run? 为了使程序运行,我需要对那些代码行做些什么? It fails to build if I leave the code as it is, and if I put two slashes in front of those lines of code, it runs but just creates a black screen. 如果我将代码保持原样,它将无法构建,并且如果在这些代码行的前面放置两个斜杠,它将运行,但只会创建黑屏。 Any help would be greatly appreciated! 任何帮助将不胜感激! Thanks! 谢谢!

import UIKit

class TaskListTableView: UITableView {

var tasks:[Task] = taskData

func viewDidLoad() {
    super.viewDidLoad()
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 60.0
}

func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

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

@IBAction func cancelToLoLFirstTableViewController(_ segue:UIStoryboardSegue) {
}

@IBAction func saveAddTask(_ segue:UIStoryboardSegue) {
    if let AddTaskTableViewController = segue.source as? AddTaskTableViewController {

        if let task = AddTaskTableViewController.task {
            tasks.append(task)

            let indexPath = IndexPath(row: tasks.count-1, section: 0)
            tableView.insertRows(at: [indexPath], with: .automatic)
        }
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
    -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath)
            as! TaskCell


        let task = tasks[indexPath.row] as Task
        cell.task = task
        return cell
}

}

I think you are confused about what the UITableView and UITableViewController classes do. 我认为您对UITableViewUITableViewController类的功能感到困惑。

A UITableView class is the class that draws the table view component you see on your screen, updates it when necessary and interprets your taps and scrolls into something that makes sense in terms of a table. UITableView类是一个类,它绘制您在屏幕上看到的表视图组件,并在必要时更新它,并解释您的点击并滚动到对表有意义的内容。 UITableView extends UIView . UITableView扩展了UIView

The UITableViewController class is designed to manage a UITableView instance, providing data to display and responding to the actions that it generates. UITableViewController类旨在管理UITableView实例,提供数据以显示和响应其生成的动作。 It is an extension of the UIViewController class. 它是UIViewController类的扩展。

UIView and UIViewController are completely different things and perform completely different functions. UIViewUIViewController是完全不同的事物,并且执行完全不同的功能。 You cannot cut and paste code from one to the other because they won't understand it. 您不能将代码从一个剪切到另一个,因为他们不会理解。

So my first recommendation is to hit the books. 所以我的第一个建议是打书。 Read up on what UIView and UIViewController 's do to get an understanding of their place in the iOS universe and how they relate to each other. 阅读有关UIViewUIViewController的操作的信息,以了解它们在iOS宇宙中的位置以及它们之间的相互关系。 Then look at UITableView and UITableViewController . 然后查看UITableViewUITableViewController

Secondly, as to your problem of wanting to have a screen with a table view as well as other components. 其次,关于您希望拥有一个带有表格视图的屏幕以及其他组件的问题。 There are multiple ways to do this and the best solution will vary depending on the complexity of the UI you are trying to build and the data and code behind it. 有多种方法可以执行此操作,并且最佳解决方案会因要构建的UI的复杂性以及其背后的数据和代码而异。

Once you've got your head around how views and controllers work. 一旦掌握了视图和控制器的工作原理。 I would start by building a simple screen with several simple components on it and a single controller behind it. 我将从构建一个简单的屏幕开始,上面有几个简单的组件,后面是一个控制器。 ie. 即。 A class you have written that extends UIViewController . 您编写的扩展UIViewController

When your happy with this, you have two choices that I typically see: 当您对此感到满意时,通常会有两种选择:

  1. You can add a UITableView UI component and set your UITableViewController as it's dataSource <UITableViewDataSource> and delegate <UITableViewDelegate> . 您可以添加UITableView UI组件,并将UITableViewController设置为dataSource <UITableViewDataSource>delegate <UITableViewDelegate> Then add in the various methods from these protocols to define the data to display and how your controller will respond to your actions on the table view. 然后添加这些协议中的各种方法,以定义要显示的数据以及控制器将如何响应表视图上的操作。

  2. A more complex choice you should look at if #1 gets too messy. 如果#1过于混乱,您应该查看一个更复杂的选择。 This choice can result in cleaner code. 此选择可以使代码更简洁。 Instead of adding the UITableView UI component, you add a UIContainerView UI component. 代替添加UITableView UI组件,而是添加UIContainerView UI组件。 UIContainerView s are designed to link to a seperate controller and view. UIContainerView旨在链接到单独的控制器和视图。 Effectively this means you have two controllers. 实际上,这意味着您有两个控制器。 One for the general UI components and one for the table view. 一种用于常规UI组件,另一种用于表格视图。

This is all going to depend on exactly what you are trying to do. 这完全取决于您要尝试执行的操作。 But first you need to do some reading. 但是首先,您需要阅读一些内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM