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

use*_*112 9 uitableview ios swift

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

我的故事板里有什么要做的?在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
}
Run Code Online (Sandbox Code Playgroud)

结果如下: 在此输入图像描述

后来,当我使用真实数据时,我将错过前2个数据行...在创建静态单元格后,我可以以某种方式"重置"行计数器吗?

我怎样才能修改2个静态单元?用于添加文本字段和标签?或者我必须以编程方式执行此操作?

use*_*112 19

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

我的解决方案看起来像这样:

1. 在此输入图像描述

  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;
    } 
    
    Run Code Online (Sandbox Code Playgroud)

最终结果将如下所示:

在此输入图像描述

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


Sye*_*man 4

您可以使用类似的方法来使用或显示静态单元格

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return numberOfDynamicCells + 1;
}
Run Code Online (Sandbox Code Playgroud)

在您的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;
}
Run Code Online (Sandbox Code Playgroud)

这是 swift 的代码。

func numberOfRowsInSection(_ section: Int) -> Int{
return numberOfDynamicCells + 1
}
Run Code Online (Sandbox Code Playgroud)

在您的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;
}
Run Code Online (Sandbox Code Playgroud)

祝你好运...