简体   繁体   English

ios swift:如何在动态tableview中拥有一个静态单元格?

[英]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。 在此输入图像描述

  1. Add and layout the static cells: 添加和布局静态单元格: 在此输入图像描述

  2. Give each cell a unique name and add them as outlet to the TableViewCell class 为每个单元格指定一个唯一的名称,并将它们作为插件添加到TableViewCell类中

  3. 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.

相关问题 如何在swift中的动态tableView中拥有多个单元格大小? - How to have multiple cell sizes in a dynamic tableView in swift? 如何在iOS Swift中禁用一个特定的tableView单元格 - How to disable one specific tableView cell in ios Swift 如何将静态单元格拖入tableView swift? - How to drag a static cell into tableView swift? Swift4 tableview 单元格高度不是动态的,始终是静态的 - Swift4 tableview cell height not dynamic and always static 如何在iOS中创建具有动态表格高度的动态表格单元格 - How to create dynamic tableview cell with dynamic tableview height in iOS 如何在Swift iOS的TableView单元格中对齐文本 - How to align text in tableview cell in swift ios 具有静态TableView单元的IOS 8动态类型 - 基本和字幕 - IOS 8 Dynamic Type with Static TableView Cell - Basic and Subtitle 如何使用Swift在静态tableview的底部添加动态Tableview? - How to add Dynamic Tableview at the bottom of static tableview using Swift? 在我的ios swift TableView中创建2个动态单元原型,具有不同的高度 - Creating 2 dynamic cell prototypes in my ios swift TableView with different heights IOS Swift 5 为动态高度 TableView Cell 添加自定义 TableViewCell 分隔符 - IOS Swift 5 Add Custom TableViewCell Separator for Dynamic Height TableView Cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM