swift - 如何将自定义单元格添加到动态生成的 TableView ?

标签 swift uitableview

将近一周的时间里,我试图弄清楚如何将静态/自定义 tableviewcell 附加到动态生成的 tableview。我根据从数据库中获取的数据填充单元格。基本上我想要完成的就像应用程序 ClassDojo 中的下图一样: enter image description here

正如您可能知道和看到的,您可以使用 ClassDojo 应用程序添加任意数量的组,但最新的单元格,在本例中,Voeg een nieuwe klas toe 将始终停留在表格 View 的底部。这正是我想要做的。

到目前为止我一直在尝试计算 TableView 中的最新单元格并尝试附加我的自定义单元格,但不幸的是我无法理解它。

如果有人能帮我解决这个问题,我将不胜感激。

提前致谢。

如果你们需要任何代码,请告诉我。

--------编辑后的帖子--------

感谢@Slayter,我确实完成了分配我的自定义单元格,但现在我面临的问题是我的自定义单元格立即被我动态创建的单元格(使用 Alamofire)覆盖。

如有任何帮助,我们将不胜感激。

最佳答案

这里是 ClassDojo iOS 工程师!非常乐意分享我们是如何做到这一点的。

Slayter提到,我们做

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return myData.count + 1 // Add one for your custom cell
}

但除此之外我们还做了以下事情:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = nil;

    if self.indexIsForCustomCell(indexPath) {
        cell = tableView.dequeueReusableCellWithIdentifier(CustomCell.reuseIdentifier)
        // Additional configuration
    } else {
        cell = tableView.dequeueReusableCellWithIdentifier(RegularCell.reuseIdentifier)
        // Additional configuration
    }

    return cell
}

CustomCell 看起来像这样(不准确):

class CustomCell: UITableViewCell {
    static let reuseIdentifier = "CustomCell"

    // More code
}

常规单元格如下所示:

class RegularCell: UITableViewCell {
    static let reuseIdentifier = "RegularCell"

    // More code
}

你的动态单元格被覆盖的原因是因为这条线

let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as UITableViewCell

请注意,无论是自定义单元格还是常规单元格,您都在使用相同的 cellIdentifier。这意味着您很可能正在执行以下操作:

self.tableView.registerClass(RegularCell.class, forIdentifier: "new group")
self.tableView.registerClass(CustomCell.class, forIdentifier: "new group") 

什么时候应该做:

self.tableView.registerClass(RegularCell.class, forIdentifier: RegularCell.reuseIdentifier)
self.tableView.registerClass(CustomCell.class, forIdentifier: CustomCell.reuseIdentifier)

self.tableView.registerClass(RegularCell.class, forIdentifier: "regularCellReuseIdentifier")
self.tableView.registerClass(CustomCell.class, forIdentifier: "customCellReuseIdentifier")

通过使用相同的标识符键,您告诉 UITableView 将两个单元格视为同一类型。因此,当它需要为在屏幕上绘制的新单元格回收内存时,它将交替使用 RegularCellCustomCell

希望这对您有所帮助,感谢您查看我们的应用程序!

================= 编辑 =================

意识到我忘记添加 indexIsForCustomCell 方法。在这里:

func indexIsForCustomCell(indexPath : NSIndexPath) -> Bool {

    if self.myData.count > 0 {
        return indexPath.section == 0 && indexPath.row == (self.myData.count+1)
    }

    return indexPath.section == 0 && indexPath.row == 0
}

关于swift - 如何将自定义单元格添加到动态生成的 TableView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36211815/

相关文章:

ios - 如何用时间间隔动画调用 n 个 subview

ios - 如何在 Swift 项目中使用 iOS SDK 插入 firebase 服务器时间戳

ios - TableView : how to get a string from selected row?

ios - UITableView didSelectRowAt 无法获取我的单元格

ios - 在 IOS swift 中删除 Stack Layout View 及其 subview 之间的链接/约束

swift - 如何从嵌入式 View 进入标签栏?

swift - 当 View 确实出现时刷新 map 数据

iphone - 在iphone中的特定 View 中确定一个 View

ios - JSON 文件直到 UITableView 才加载

ios - UITableView 头部 View 动画