自定义UITableViewCell中的UILabel永远不会被初始化

时间:2014-08-08 10:27:58

标签: ios uitableview swift

我在使用Xcode6(beta 4)在swift中创建自定义表格视图时遇到问题。更确切地说,我无法在单元格内访问(解包)我的自定义UILabel,因为它们从未被初始化。

以下是我完成所有设置的方法:

我在故事板中创建了一个视图,其中包含一个带有原型单元格的表格视图:

How it looks in 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)
    }
}

下面是我在实用程序窗格(故事板)中配置单元格的图片:

Utilities Pane

当我想访问UILabels时,问题出现在函数tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> CourseTableViewCell内。如果我要放cell.titleLabel.text = "Course Title"之类的内容,我会收到以下错误消息:

fatal error: unexpectedly found nil while unwrapping an Optional value

我在哪里做错了?非常感谢任何帮助,谢谢!

2 个答案:

答案 0 :(得分:1)

正如UITableView Using Swift中的最后一个答案所解释的,这是一个Xcode错误。添加以下内容:

    override func tableView(tableView:UITableView!, heightForRowAtIndexPath indexPath:NSIndexPath)->CGFloat {
    return 44
}

(或你想要的任何高度),你会没事的。 我复制了您的代码并验证了解决方案。

道歉,我忘记了其他事情:你不能registerClass:forCellReuseIdentifier。在界面构建器中设置表格和原型单元格时,这已经完成。

答案 1 :(得分:0)

很抱歉在经过这么多时间后回复,但整个周末我都在解决这个问题。这个bug仍然存在于XCode 8.3.2(8E2002)上,这是我编写这些行时的最新版本。我遇到与OP完全相同的问题并添加

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 44
}

对我不起作用。对我有用的唯一方法就是从我的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 v v v v v        
    self.tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "MyCustomCell")
    // REMOVE THIS ^ ^ ^ ^ ^ 
    // Do whatever you want here…
}

heightForRowAt indexPath无效,您无需使用它来修复它。

我希望这也能帮助别人!