TableView Swift 3中的不同Cell [英] Different Cell in TableView Swift 3

查看:299
本文介绍了TableView Swift 3中的不同Cell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为初学者,我正在尝试使用 UITableView IOCollectionView ,如何创建不同的单元格(某些单元格有一个集合视图,有些单元格只有文本或图像,...)在同一个容器中?

As a beginner, I'm trying to work with UITableView and IOCollectionView, How can I create different cells (some cells have a collection View and some cells have text or image only,...) in the same container?

例如:Appstore,上面的单元格top是一个横幅,包含广泛的Collection View,第二个单元格包含一个类别,其他包含标签或按钮。

For example: the Appstore, the cell on the top is a banner, containing wide Collection View, second cell containing a category a the others containing label or button.

我使用swift 3并且更喜欢使用storyboard。

I work with swift 3 and prefer to use storyboard.

推荐答案

假设您确实知道如何创建自定义单元格(如果您不检查这个问题)并实现所需的数据源方法,你应该在 cellForRowAt cellForItem 方法 - 我在代码片段中使用 cellForRowAt - :

Assuming that you do know how to create your custom cell (if you don't check this question) and implementing the required data source methods, you should do this in cellForRowAt or cellForItem methods -I'm using cellForRowAt in the code snippet-:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // first row should display a banner:
        if indexPath.row == 0 {
            let bannerCell = tableView.dequeueReusableCell(withIdentifier: "BannerTableViewCell") as! BannerTableViewCell

            // ...

            return bannerCell
        }

        // second row should display categories
        if indexPath.row == 1 {
            let categoriesCell = tableView.dequeueReusableCell(withIdentifier: "CategoriesTableViewCell") as! CategoriesTableViewCell

            // ...

            return categoriesCell
        }

        // the other cells should contains title and subtitle:
        let defaultCell = tableView.dequeueReusableCell(withIdentifier: "CategoriesTableViewCell") as! TileAndSubtitleTableViewCell

        // ...

        return defaultCell
    }

让它更具可读性:

您还可以定义枚举用于检查 indexPath.row 而不是将它们与ints进行比较:

you can also define enum for checking the indexPath.row instead of compare them to ints:

enum MyRows: Int {
    case banner = 0
    case categories
}

现在,您可以与可读值进行比较:

Now, you can compare with readable values:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // first row should display a banner:
        if indexPath.row == MyRows.banner.rawValue {
            let bannerCell = tableView.dequeueReusableCell(withIdentifier: "BannerTableViewCell") as! BannerTableViewCell

            // ...

            return bannerCell
        }

        // second row should display categories
        if indexPath.row == MyRows.categories.rawValue {
            let categoriesCell = tableView.dequeueReusableCell(withIdentifier: "CategoriesTableViewCell") as! CategoriesTableViewCell

            // ...

            return categoriesCell
        }

        // the other cells should contains title and subtitle:
        let defaultCell = tableView.dequeueReusableCell(withIdentifier: "CategoriesTableViewCell") as! TileAndSubtitleTableViewCell

        // ...

        return defaultCell
    }

这篇关于TableView Swift 3中的不同Cell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆