gpt4 book ai didi

ios - 动态原型(prototype)表中需要一些静态单元格

转载 作者:搜寻专家 更新时间:2023-11-01 05:37:22 25 4
gpt4 key购买 nike

您好,在我的 TableViewController 中,我首先需要三个静态单元格,然后是动态单元格。我做了一些研究,发现我可以在动态表原型(prototype)中做静态单元格,但反之则不行。

所以我拖了一个 tableViewController 并选择动态原型(prototype)并添加了 4 个原型(prototype)单元格(3 个静态单元格和 1 个动态单元格)并为它们中的每四个赋予了不同的标识符,并且还向所有四个原型(prototype)单元格添加了 TableViewCell 类。我还创建了一个 TableViewCell 类。

现在我收到这个错误

fatal error: unexpectedly found nil while unwrapping an Optional value 

这是我的代码

 let NUMBER_OF_STATIC_CELLS = 3
var labels = ["label1","label2","label3"]

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


return labels.count + 1



}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {



var cell: TestViewCell!

print(indexPath.row)
if (indexPath.row == 0) {
cell = tableView.dequeueReusableCellWithIdentifier("static1", forIndexPath: indexPath) as! TestViewCell


}

if (indexPath.row == 1) {
cell = tableView.dequeueReusableCellWithIdentifier("static2", forIndexPath: indexPath) as! TestViewCell
//cell.cardSetName?.text = self.cardSetObject["name"] as String
}

if (indexPath.row == 2) {
cell = tableView.dequeueReusableCellWithIdentifier("static2", forIndexPath: indexPath) as! TestViewCell
//cell.cardSetName?.text = self.cardSetObject["name"] as String
}





cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TestViewCell
cell.testLabel.text = "row \(indexPath.row)" // return test rows as set in numberOfRowsInSection


return cell; //getting error here

}

更新

好的错误已被删除。现在我想出了另一个问题。我想知道是否总共有 4 行。 3 个是静态的,其余的将是动态的。那么总行数是多少。我在做这个

labels.count+2 //2 static cells(0 1 2 )

但是我得到了 fatal error :数组索引超出范围

最佳答案

如果行是 4 或更多,您没有代码返回单元格。改变这个:

if (indexPath.row == 3) {

为此:

if (indexPath.row >= 3) {

或者只是将其更改为:

else {

关于ios - 动态原型(prototype)表中需要一些静态单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35384351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com