首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Swift中的简单UITableView -意外发现零

Swift中的简单UITableView -意外发现零
EN

Stack Overflow用户
提问于 2014-07-18 20:36:21
回答 13查看 33.5K关注 0票数 17

非常简单的代码:

代码语言:javascript
复制
func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
    return 1
}

func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int {
    return 5
}


func tableView(tableView:UITableView!, cellForRowAtIndexPath indexPath:NSIndexPath!) -> UITableViewCell! {
    let cell: BookTableViewCell = BookTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "BookCell")
    println("ip: \(indexPath.row)")
    cell.bookLabel.text = "test"

    return cell
}

在cell.bookLabel.text行中,我得到了以下内容:

代码语言:javascript
复制
fatal error: unexpectedly found nil while unwrapping an Optional value

BookTableViewCell的定义如下:

代码语言:javascript
复制
class BookTableViewCell: UITableViewCell {

    @IBOutlet var bookLabel: UILabel

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

bookLabel被正确地连接在故事板的原型单元格中。我为什么要犯这个错误?

EN

回答 13

Stack Overflow用户

回答已采纳

发布于 2014-07-18 20:59:16

当您在代码中创建视图时,它的IBOutlet属性不能正确地连接起来。你想要从dequeueReusableCellWithIdentifier得到的版本

代码语言:javascript
复制
let cell = tableView.dequeueReusableCellWithIdentifier("BookCell") as BookTableViewCell
票数 7
EN

Stack Overflow用户

发布于 2014-10-14 00:08:58

如果您使用的是故事板,请确保在文件开始时没有这一行:

代码语言:javascript
复制
self.tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "customCell")

它将覆盖故事板,从而忽略故事板中的出口链接。

票数 85
EN

Stack Overflow用户

发布于 2015-04-08 01:30:17

我收到这个错误是因为我没有在自定义单元格的童话板中写入标识符。

还请确保它与以下代码相匹配:

代码语言:javascript
复制
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {

        let cell = tableView.dequeueReusableCellWithIdentifier("CustomTableCell") as CustomTableCell

        ...
    }
票数 20
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24833391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档