我想使用 UITableView
在动态单元格列表的顶部有 2 个静态单元格。据我了解,我必须使用动态原型(prototype) tableView。但我不明白如何添加 2 个静态单元格并设计它们,例如。向第一个添加文本字段,向第二个添加标签。
我必须在 Storyboard中做什么?我在 Controller 中必须做什么?如何区分静态和动态单元格?
编辑: 我试过这个进行测试:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cardCell", forIndexPath: indexPath) as CardTableViewCell
//static cell
if (indexPath.row < 2) {
cell.dyn.text = "static \(indexPath.row)"
return cell;
}
// Configure the cell...
cell.dyn.text = "buh"
return cell
}
结果是:
稍后当我使用真实数据时,我会错过前 2 行数据...... 我可以在创建静态单元格后以某种方式“重置”行计数器吗?
我该如何修改这两个静态单元格?用于添加文本字段和标签?还是我必须以编程方式执行此操作?
最佳答案
我在这里找到了帮助:Mixing static and dynamic sections in a grouped table view
我的解决方案是这样的:
1.
添加和布局静态单元格:
为每个单元格指定一个唯一的名称,并将它们作为导出添加到
TableViewCell
类调整代码:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 3 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if (section == 2){ // my dynamic cell is index 2 return 5 // just for testing, add here yourrealdata.count } return 1 // for static content return 1 } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell: CardTableViewCell! if (indexPath.section == 0) { cell = tableView.dequeueReusableCellWithIdentifier("static1", forIndexPath: indexPath) as CardTableViewCell cell.cardSetName?.text = self.cardSetObject["name"] as String }else if (indexPath.section == 1) { cell = tableView.dequeueReusableCellWithIdentifier("static2", forIndexPath: indexPath) as CardTableViewCell // just return the cell without any changes to show whats designed in storyboard }else if (indexPath.section == 2) { cell = tableView.dequeueReusableCellWithIdentifier("cardCell", forIndexPath: indexPath) as CardTableViewCell cell.dyn.text = "row \(indexPath.row)" // return test rows as set in numberOfRowsInSection } return cell; }
最终结果将如下所示:
我希望我能帮助有同样问题的人:)
关于ios - 如何在动态 TableView 中有一个静态单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28942400/