简体   繁体   English

TableView Swift 3中的不同Cell

[英]Different Cell in TableView Swift 3

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

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. 例如:Appstore,顶部的单元格是一个横幅,包含宽的Collection View,第二个单元格包含一个类别,其他包含标签或按钮。

I work with swift 3 and prefer to use storyboard. 我使用swift 3并且更喜欢使用故事板。

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-: 假设您确实知道如何创建自定义单元格(如果不检查此问题 )并实现所需的数据源方法,则应在cellForRowAtcellForItem方法中执行此操作 - 我在代码片段中使用cellForRowAt

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
    }

Make it more readable: 使其更具可读性:

you can also define enum for checking the indexPath.row instead of compare them to ints: 您还可以定义用于检查indexPath.row enum ,而不是将它们与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
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM