Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Uitableview 如何在没有原型单元的情况下创建tableview_Uitableview_Swift - Fatal编程技术网

Uitableview 如何在没有原型单元的情况下创建tableview

Uitableview 如何在没有原型单元的情况下创建tableview,uitableview,swift,Uitableview,Swift,如果我不想在tableview中使用原型单元格。在cellforrowatinexpath中,我是否仍需要在代码中提供单元格标识符以将单元格出列 我已经尝试过了,但是当tableview尝试填充时,它会抛出一个运行时错误: 致命错误:在展开可选值时意外发现nil 它发生在这一行: let cell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell 我不确定要使用哪一行将单元格出列。您需要将表视图注

如果我不想在tableview中使用原型单元格。在
cellforrowatinexpath
中,我是否仍需要在代码中提供单元格标识符以将单元格出列

我已经尝试过了,但是当tableview尝试填充时,它会抛出一个运行时错误:

致命错误:在展开可选值时意外发现nil

它发生在这一行:

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

我不确定要使用哪一行将单元格出列。

您需要将表视图注册到该单元格类

tableView.registerClass(_ cellClass: AnyClass, forCellReuseIdentifier identifier: String)
,然后您可以尝试将单元格出列。如果没有要出列的单元格,则需要创建一个新的单元格


但是这里的主要问题应该是:
dequeueReusableCellWithIdentifier
返回一个
可选的
。如果您想将其作为单元格返回,它可能会给出错误信息。您应该确保在
cellforrowatinexpath
函数中返回
非可选的
单元格。

是的,您必须返回,否则swift无法找到您想要
退出队列的对象类型,因此您得到了
nil
对象

你应该使用
-dequeueReusableCellWithIdentifier:forIndexPath:

而不是
-dequeueReusableCellWithIdentifier

这是因为第一个函数实际上返回
AnyObject
,而
dequeue
不带indexath返回
AnyObject?

为了使代码正常工作,您可以将代码更改为
…forIndexPath

或者这样做:

var cell: UITableViewCell?
if let cell = cell {
    //dequeue here
} else {
    cell = UITableViewCell(style: UITableViewCellStyle,
reuseIdentifier reuseIdentifier: String?) ...
}
//config cell
return cell
如果你是一个喜欢用代码做所有事情的人,你需要像Mert建议的那样将你的
UITableViewCell
类注册到
tableView

tableView.registerClass(UITableViewCell.self,forCellReuseIdentifier标识符:“Cell”)


示例代码:

// I remove all other things, just focus on what you need
override func viewDidLoad() {
        super.viewDidLoad()
        table.delegate = self
        table.dataSource = self
        table.registerClass(UITableViewCell.self, forCellReuseIdentifier identifier: "Cell")
    }

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
        configureCell(cell, atIndexPath: indexPath)
        return cell
    }

当我尝试你的注册类代码时,我得到一个错误,应该是“,”。另外,主要的问题不是你说了什么,而是你从哪里得到标识符字符串(因为这在IB中不存在)。“,”应该不是一个真正的错误,你只是写了一些错误,这与您使用的函数无关。代码中的下划线导致“,”错误。您不能使用字符串?。您需要在那里提供一个有效字符串,否则它将无法编译。您可以发布出列代码吗(您在那里只有一条注释)?我从文档中复制了它,它将
reuseIdentifer
写成
String?
。真奇怪。好吧,检查我的示例代码,它应该可以工作。这适用于swift 1.2,如果您使用swift 1.1,请将
更改为
作为
大量错误:“对registerClass使用未解析标识符'forCellReuseIdentifier'”。还包括“使用未解析标识符'configureCell'”。如果您先尝试代码,可能是最好的。有点像现在往墙上扔泥巴。:D我说我把所有其他东西都拿走了:D让我解释一下:
configCell
是一个
私有函数,你可以在
单元格中设置东西,比如图像、文本、文本、文本标签和
detailtextlab
。未解析的标识符错误不应该出现,因为我们注册并使用了相同的
reuseIdentifier
字符串(如代码中所示)。我猜你在故事板里有什么东西,或者你设置表格的代码?不然后你应该发布一些示例代码,我会帮你找到:)