[英]ios swift: How to have one static cell in a dynamic tableview?
I would like to use a tableView
to have 2 static cells on top of a list of dynamic cells. 我想使用tableView
在动态单元列表的顶部有2个静态单元格。 As far as I understand, I have to use a dynamic prototype tableView. 据我了解,我必须使用动态原型tableView。 But I don't understand how to add 2 static cells and design them, eg. 但我不明白如何添加2个静态单元并设计它们,例如。 adding a textfield to the first and a label to the second. 将文本字段添加到第一个,将标签添加到第二个。
What do I have to do in my storyboard? 我的故事板里有什么要做的? And what do I have to do inside the Controller? 在Controller内部我需要做什么? How can I differentiate the static from the dynamic cells? 如何区分静态和动态细胞?
Could someone give me as a bloody beginner some guidance where to start? 有人可以给我一个血腥的初学者一些指导从哪里开始? Thanks so much!! 非常感谢!!
EDIT: I tried this for testing: 编辑:我试过这个测试:
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
}
this results in this: 结果如下:
Later when I use real data I will miss the first 2 data rows... Can I somehow "reset" the row counter after I created my static cells? 后来,当我使用真实数据时,我将错过前2个数据行...在创建静态单元格后,我可以以某种方式“重置”行计数器吗?
And how can I modify the 2 static cells? 我怎样才能修改2个静态单元? For adding a textfield and labels? 用于添加文本字段和标签? Or do I have to do this programmatically? 或者我必须以编程方式执行此操作?
I found help here: Mixing static and dynamic sections in a grouped table view 我在这里找到了帮助: 在分组表视图中混合静态和动态部分
And my solution looks like this: 我的解决方案看起来像这样:
1. 1。
Add and layout the static cells: 添加和布局静态单元格:
Give each cell a unique name and add them as outlet to the TableViewCell
class 为每个单元格指定一个唯一的名称,并将它们作为插件添加到TableViewCell
类中
Adjust the code: 调整代码:
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; }
End results will look like this: 最终结果将如下所示:
I hope I can help someone with the same question :) 我希望我可以帮助有同样问题的人:)
you could use something like this to use or display your static cell 你可以使用这样的东西来使用或显示你的静态细胞
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return numberOfDynamicCells + 1;
}
and in you cellForRowAtIndexPath
datasource you may use something like this. 在你的cellForRowAtIndexPath
数据源中你可以使用这样的东西。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 0){
// go ahead to display your static Cell
}
else{
//go ahead to display your dynamic cells.
}
return yourCell;
}
here is code for swift. 这是swift的代码。
func numberOfRowsInSection(_ section: Int) -> Int{
return numberOfDynamicCells + 1
}
and in you cellForRowAtIndexPath
datasource you may use something like this. 在你的cellForRowAtIndexPath
数据源中你可以使用这样的东西。
func cellForRowAtIndexPath(_ indexPath: NSIndexPath) -> UITableViewCell?{
if indexPath.row = 0{
// go ahead to display your static Cell
}
else{
//go ahead to display your dynamic cells.
}
return yourCell;
}
Good Luck... 祝好运...
Yes you can by having static cells be IBOutlet properties and in the tableView(_:cellForRowAt:)
you can return those properties for any index paths you want 是的,您可以通过将静态单元格设置为IBOutlet属性,并且在tableView(_:cellForRowAt:)
您可以为所需的任何索引路径返回这些属性
Here's an example: 这是一个例子:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row {
case 0:
return firstStaticCell
case 1:
return secondStaticCell
default:
let cell = tableView.dequeueReusableCell(withIdentifier: "DynamicCell", for: indexPath)
cell.textLabel?.text = "Dynamic \(indexPath.row + 1)"
return cell
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.