gpt4 book ai didi

ios - 多个原型(prototype)单元 - Swift

转载 作者:可可西里 更新时间:2023-11-01 02:23:00 24 4
gpt4 key购买 nike

我在创建多个原型(prototype)单元格时遇到问题...基本上我需要 2 个静态单元格和 1 个原型(prototype)单元格,它们将根据数据输入添加新行。由于您无法创建静态和原型(prototype)表,因此我尝试使用 3 个原型(prototype)单元格来实现。使用下面的代码,运行时我的表中不会显示任何内容。它只显示一个空白表。

TableView Controller :

class NewTableViewController: UITableViewController,UITableViewDelegate, UITableViewDataSource {

var testData = [row1, row2, row3]

override func viewDidLoad() {
super.viewDidLoad()
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 3
}


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (section == 2){
return testData.count
}
return 1
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

var cell: CustomCell!

if (indexPath.section == 0) {
cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! CustomCell

cell.myLabel.text = "Static Label"

}else if (indexPath.section == 1) {
cell = tableView.dequeueReusableCellWithIdentifier("addCell", forIndexPath: indexPath) as! CustomCell
cell.addLabel.text = "Add New Data"

}else if (indexPath.section == 2) {
cell = tableView.dequeueReusableCellWithIdentifier("protoCell", forIndexPath: indexPath) as! CustomCell
cell.protoLabel.text = "row \(indexPath.row)"
}

return cell;
}
}

自定义单元格:

class CustomCell: UITableViewCell {

@IBOutlet weak var myLabel: UILabel!

@IBOutlet weak var addLabel: UILabel!

@IBOutlet weak var protoLabel: UILabel!
}

最佳答案

确保注册自定义单元格以在 viewDidLoad 中使用

var nib = UINib(nibName: "CustomTableViewCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "customCell")

您也只有一个自定义单元格,因此您应该只在 cellForRowAtIndexPath 中使用一个重用标识符。

关于ios - 多个原型(prototype)单元 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29827973/

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