ios - UITableView 单元格不显示
问题描述
import UIKit
class ToDoViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var DateLabel: UILabel!
@IBOutlet weak var MonthLabel: UILabel!
@IBOutlet weak var DayLabel: UILabel!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var newButton: UIImageView!
static var ToDoEvents:[event] = []
override func viewDidLoad() {
super.viewDidLoad()
ToDoViewController.readFromFile()
let nib = UINib(nibName: "CustomCell", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "customCell")
}
public static func readFromFile(){
ToDoViewController.ToDoEvents.append(event(eventName: "Spanish Workbook", eventLength: 3601, complete: false))
ToDoViewController.ToDoEvents.append(event(eventName: "History Test", eventLength: 37, complete: false))
ToDoViewController.ToDoEvents.append(event(eventName: "Lit Essay", eventLength: 40, complete: false))
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ToDoViewController.ToDoEvents.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell") as! CustomCell
let currentEvent = ToDoViewController.ToDoEvents[indexPath.row]
let eventName = currentEvent.name!
let eventLength = currentEvent.time!
let completion = currentEvent.isComplete!
print(eventName)
print(eventLength)
print(completion)
cell.customInit(name: "\(eventName)", time: "\(eventLength)", completed: completion)
return cell
}
}
目前,我正在尝试创建一个带有 ToDo 列表的应用程序。我正在使用 UITableView 来显示信息,并且我有一个名为“CustomCell”的自定义 UITableViewCell,用于在表格视图中显示信息。但是,当我运行此代码时,屏幕上没有显示任何内容。我还在 CellForRowAtIndexPath 方法中添加了一些打印语句,这些打印语句没有被执行。然后我尝试添加
self.tableView.delegate = self
self.tableView.dataSource = self
但是,当我尝试使用此附加代码运行应用程序时,应用程序崩溃并出现以下错误:
Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason:
'Could not load NIB in
bundle: 'NSBundle
</Users/sid/Library/Developer/CoreSimulator
/Devices/812843C0-ABBA-43E9-A391-
0BED3CBB5030/data/Containers/Bundle
/Application/F8DABFF7-80ED-49E1-8760-315C3C99AFB1/TimeIt.app>
(loaded)' with name 'CustomCell''
我还观看了多个教程,这些教程概述了如何将自定义单元格添加到您的代码中,并且这些教程都没有使用 tableView.delegate = self 或 tableView.dataSource = self 代码。
我不确定我做错了什么。感谢所有帮助
先感谢您!
解决方案
我按照以下步骤修复了错误:
- 打开导致问题的 XIB 文件
- 单击左侧栏上的文件所有者图标(顶部一个,看起来像一个黄色轮廓框)
- 如果您没有看到右侧边栏,请单击工具栏中“查看”上方的第三个图标。
- 这将显示右侧边栏 在右侧边栏中,单击第三个选项卡 - 看起来有点像报纸的选项卡 在顶部的“自定义类”下,确保 Class 是应显示的 ViewController 的名称对应这个观点。
- 如果没有,请输入它在右侧边栏中,单击最后一个选项卡 - 看起来像一个带有箭头的圆圈,您应该会看到“outlets”和“view”。
- 将旁边的圆圈拖到左侧栏上的“查看”图标(底部一个,看起来像一个带有粗灰色轮廓的白色正方形。
保存xib并重新运行
推荐阅读
- android - 如何使用 xml 或代码将圆形进度条划分为多个部分?
- r - 根据条件将列除以值
- mysql - Docker Compose:第 1 行的 MySQL 语法错误“DELIMITER”
- python - 您可以在 PYPI 上与多个用户一起上传吗
- php - Laravel 5.5 LengthAwarePaginator 一旦传递给视图就会转换为集合
- java - 在 Android 上的 MapBox 中的 2+ 个标记/wievpoints(非动态)之间绘制线
- opengl - OpenGL - 如何在不旋转物体的同时旋转光源?
- java - 设置断点以覆盖 IntelliJ 中方法的所有返回语句
- javascript - 找不到模块:错误:无法解析“css-loader”
- python - 如何列出自定义的烧瓶命令