首页 > 解决方案 > 无法为 UITableView 制作插座

问题描述

我试图在 UIViewController 中放置一个 tableview,但我无法控制从 UITableView 拖动到我的视图控制器中以创建一个插座。当我尝试拖动时,它拒绝提示插座。我什至尝试自己制作一个插座,然后从视图控制器类拖到表格视图,但它拒绝链接。

这是我的工作区

这是我的 Tableview 课程:

季节单元班

import UIKit

class SeasonCell: UITableViewCell {
     @IBOutlet weak var seasonLabel: UILabel!
}

季节班

class Season: NSObject {
    var name: String

    init(name: String){
        self.name = name
    }

}

视图控制器

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var seasons = [Season]()

    //created myself, wont link
    @IBOutlet weak var seasonsTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        seasons.append(Season(name: "2018 Cross Country Season"))
        seasonsTableView.delegate = self
        seasonsTableView.dataSource = self
    }

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

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

        // Configure the cell...
        let thisSeason = seasons[indexPath.row]
        cell?.seasonLabel?.text = thisSeason.name

        return cell!
    }


}

标签: iosswiftuitableview

解决方案


您需要 1) 给视图控制器的类一个唯一的名称。这反过来又创建了一个类型。2)然后您需要打开 Storyboard,并将包含 tableview 的视图控制器的类设置为您刚刚创建的类型。这将允许您使用控件拖动创建一个 Outlet。

在storyboard中设置视图控制器类型的方法如下:

在此处输入图像描述


推荐阅读