ios - swift 。为什么 UITableView 委托(delegate)不起作用?

标签 ios swift uitableview

我试图理解非常基本的东西。
我开始使用 UITable 元素以及如何在我的代码中实现它。我找到了很多关于 UITable 委托(delegate)的简单示例。但是当我尝试在具有独立 ViewController 和类的代码中使用它时,从未调用 UITableView 委托(delegate)。我发现有必要在我的类里面初始化那个委托(delegate)。在那之后,它奏效了。我的问题是,为什么当我使用 UITableView 运行简单示例且没有初始化委托(delegate)时它可以工作,但在我的代码中我必须初始化它?
示例代码:

    import UIKit

class ViewController: UIViewController, UITextFieldDelegate {
    
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var addVideoTextField: UITextField!
    
    var videos: [String] = ["Facebook Interview 3",
                            "iOS Podcasts",
                            "UIButton Animations"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.tableFooterView = UIView(frame: CGRect.zero)
    }
    
}


extension ViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return videos.count
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let videoTitle = videos[indexPath.row]
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "VideoCell", for: indexPath)
        cell.textLabel!.text = videoTitle
        
        return cell
    }

}
我的代码:
import UIKit

class LampsViewController: UIViewController, UITextFieldDelegate {
    
    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var tableView: UITableView!
    
    var videos:[String] = ["Home", "Work", "Office"]
        
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        tableView.delegate = self
        tableView.tableFooterView = UIView(frame: CGRect.zero)
      }
        
      override func viewWillAppear(_ animated: Bool) {
      }

}

extension LampsViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return videos.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let videoTitle = videos[indexPath.row]
        
        var cell = tableView.dequeueReusableCell(withIdentifier: "LampItem", for: indexPath)
        cell.textLabel!.text = videoTitle
        return cell
    }
    
    
}

最佳答案

viewDidLoad你需要

tableView.delegate = self
tableView.dataSource = self

关于ios - swift 。为什么 UITableView 委托(delegate)不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62522753/

相关文章:

swift - 为什么这个group.wait总是成功?

ios - UITableView页脚高度问题

ios - 应用发布响应用户操作后添加 '#define xxx'

ios - 如何在不影响 UICollectionViewCell 的不透明度的情况下设置 UICollectionView 背景的不透明度?

ios - 无法将自定义颜色设置为导航栏项目 iOS Swift

view - iOS 8 - Swift - Xcode 6 Beta - 在没有界面生成器的情况下工作(没有 Nib 和 Storyboard)

ios - 添加彩色图像作为导航栏中的右侧栏按钮项

ios - 如何在 iPhone 中更改 UItableview 的高度和宽度

ios - Swift 中的自调整单元格大小,如何以编程方式进行约束?

iOS泄漏工具CGContextDrawPDFPage