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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28942400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:09:07  来源:igfitidea点击:

ios swift: How to have one static cell in a dynamic tableview?

iosuitableviewswift

提问by user1555112

I would like to use a tableViewto have 2 static cells on top of a list of dynamic cells. As far as I understand, I have to use a dynamic prototype tableView. But I don't understand how to add 2 static cells and design them, eg. adding a textfield to the first and a label to the second.

我想使用 atableView在动态单元格列表的顶部有 2 个静态单元格。据我了解,我必须使用动态原型 tableView。但我不明白如何添加 2 个静态单元格并设计它们,例如。向第一个添加文本字段,向第二个添加标签。

What do I have to do in my storyboard? And what do I have to do inside the 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: enter image description here

这导致: 在此处输入图片说明

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? For adding a textfield and labels? Or do I have to do this programmatically?

以及如何修改 2 个静态单元格?用于添加文本字段和标签?还是我必须以编程方式执行此操作?

回答by user1555112

I found help here: Mixing static and dynamic sections in a grouped table view

我在这里找到了帮助:在分组表视图中混合静态和动态部分

And my solution looks like this:

我的解决方案如下所示:

1. enter image description here

1. 在此处输入图片说明

  1. Add and layout the static cells: enter image description here

  2. Give each cell a unique name and add them as outlet to the TableViewCellclass

  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;
    } 
    
  1. 添加和布局静态单元格: 在此处输入图片说明

  2. 给每个单元格一个唯一的名称并将它们添加为TableViewCell类的出口

  3. 调整代码:

    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:

最终结果将如下所示:

enter image description here

在此处输入图片说明

I hope I can help someone with the same question :)

我希望我能帮助有同样问题的人:)

回答by Syed Ali Salman

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

祝你好运...

回答by Pitiphong Phongpattranont

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
  }
}

enter image description here

在此处输入图片说明