ios - 如何在动态 TableView 中有一个静态单元格?

标签 ios uitableview swift

我想使用 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
}

结果是:

enter image description here

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

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

最佳答案

我在这里找到了帮助:Mixing static and dynamic sections in a grouped table view

我的解决方案是这样的:

1. enter image description here

  1. 添加和布局静态单元格: enter image description here

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

最终结果将如下所示:

enter image description here

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

关于ios - 如何在动态 TableView 中有一个静态单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28942400/

相关文章:

ios - requestTestAccountTokensWithArraysOfPermissions FBSDK 4.x 版本

swift - 类型 'Int' 没有下标成员?

ios - dispatch_group_leave swift 崩溃

ios - 使用 Core Bluetooth 从计算机向 iOS 发送数据

javascript - 我无法使用 RNFS 使用从 RNCameraRoll 获取的阶段集读取文件

ios - 如何在三个 View 之间成功创建数据的向下钻取层次结构?

iphone - 带有矩阵 View 的 UITableView

ios - UIImageView 缩放以填充不剪辑 subview

ios - 将 JSON 提取到 NSArray 中以进行动态导航

ios - 在 UITableview 中快速删除行并返回到上一个 View 时,应用程序崩溃