自定义UITableViewCell中的UILabel永远不会被初始化
我在使用Xcode6(beta 4)在swift中创建自定义表视图单元时遇到了麻烦。 更确切地说,我无法在单元格内访问(展开)我的自定义UILabel,因为它们永远不会被初始化。
这是我如何完成所有设置:
我在storyboard中创建了一个视图,其中包含一个带有原型单元格的表视图:
视图连接到一个类MyCoursesTableViewController,而单元格(带有标识符courseCell)连接到CourseTableViewCell。 我在下面粘贴了两个类(只有相关的代码位):
MyCoursesTableViewController.swift
import UIKit class MyCoursesTableViewController: NavToggleTableViewController { override func viewDidLoad() { super.viewDidLoad() self.tableView.registerClass(CourseTableViewCell.self, forCellReuseIdentifier: "courseCell") } override func numberOfSectionsInTableView(tableView: UITableView!) -> Int { return 1 } override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int { return 1 } override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { var cell : CourseTableViewCell = tableView.dequeueReusableCellWithIdentifier("courseCell", forIndexPath: indexPath) as CourseTableViewCell // cell is not nil if let titleLabel = cell.titleLabel { // never gets here } else { cell.textLabel.text = "Course Title" } return cell } }
NavToggleTableViewController
类只是我用于所有视图控制器的常见基类,不会影响结果。
CourseTableViewCell.swift
import UIKit class CourseTableViewCell: UITableViewCell { @IBOutlet weak var courseIcon: UIImageView! @IBOutlet weak var teacherIcon: UIImageView! @IBOutlet weak var studentsCountIcon: UIImageView! @IBOutlet weak var titleLabel: UILabel! @IBOutlet weak var studentsCountLabel: UILabel! @IBOutlet weak var teacherLabel: UILabel! init(style: UITableViewCellStyle, reuseIdentifier: String!) { super.init(style: style, reuseIdentifier: reuseIdentifier) } override func awakeFromNib() { super.awakeFromNib() } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } }
下面是我在实用程序窗格(在故事板中)中如何配置单元格的图片:
当我想访问UILabels时,问题出现在函数tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> CourseTableViewCell
。 如果我要输入像cell.titleLabel.text = "Course Title"
我会收到以下错误消息:
fatal error: unexpectedly found nil while unwrapping an Optional value
我哪里做错了? 非常感谢任何帮助,谢谢!
正如在UITableView使用Swift的最后一个答案中所解释的那样,它是一个Xcode错误。 添加以下内容:
override func tableView(tableView:UITableView!, heightForRowAtIndexPath indexPath:NSIndexPath)->CGFloat { return 44 }
(或者你想要的任何高度)你会没事的。 我复制了您的代码并validation了解决方案。
道歉,我忘了别的东西:你不能registerClass:forCellReuseIdentifier
。 在界面构建器中设置表和原型单元时,这已经完成。
很抱歉在很长一段时间过后回复,但整个周末我都在解决这个问题。 这个bug仍然存在于XCode 8.3.2(8E2002)上,这是我编写这些行时的最新版本。 我有与OP相同的问题并添加
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 44 }
对我不起作用。 唯一对我viewDidLoad
是从我的viewDidLoad
删除它:
override func viewDidLoad() { super.viewDidLoad() // Uncomment the following line to preserve selection between presentations // self.clearsSelectionOnViewWillAppear = false // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem() // REMOVE THIS vvvvv self.tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "MyCustomCell") // REMOVE THIS ^ ^ ^ ^ ^ // Do whatever you want here… }
heightForRowAt indexPath
无效,您无需使用它来修复它。
我希望这对某人也有帮助!