swift - 如何在 Swift 中使用 switch 语句实现 collectionView 功能?

标签 swift uicollectionview

我是 swift 的新手,正在尝试在 ViewController 中实现 collectionView 函数。现在我有三种类型的 UICollectionViewCell,这是我的代码:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if collectionView == self.collectionViewCategory {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: categoryIdentifier, for: indexPath as IndexPath) as! CategoryCell

        cell.CategoryIcon.image = self.categoryItems[indexPath.item]
        cell.layer.borderColor = UIColor.lightGray.cgColor
        cell.layer.borderWidth = 1
        cell.layer.cornerRadius = 5

        return cell
    }
    else if collectionView == self.collectionViewHour {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: hourIdentifier, for: indexPath as IndexPath) as! HourCell

        cell.hourItem.text = self.hourItems[indexPath.item]
        cell.layer.borderColor = UIColor.lightGray.cgColor
        cell.layer.borderWidth = 1
        cell.layer.cornerRadius = 5

        return cell
    }
    else {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: minuteIdentifier, for: indexPath as IndexPath) as! MinuteCell

        cell.minuteItem.text = self.minuteItems[indexPath.item]
        cell.layer.borderColor = UIColor.lightGray.cgColor
        cell.layer.borderWidth = 1
        cell.layer.cornerRadius = 5

        return cell
    }
}

是否可以使用switch语句达到同样的效果?让代码看起来更优雅一点?

我希望我的代码是这样的:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell:UICollectionViewCell

    switch collectionView {
        case self.collectionViewCategory:
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: categoryIdentifier, for: indexPath as IndexPath) as! CategoryCell
            cell.CategoryIcon.image = self.categoryItems[indexPath.item]
        case self.collectionViewHour:
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: hourIdentifier, for: indexPath as IndexPath) as! HourCell
            cell.hourItem.text = self.hourItems[indexPath.item]
        default:
            //do nothing.
    }
    cell.layer.borderColor = UIColor.lightGray.cgColor
    cell.layer.borderWidth = 1
    cell.layer.cornerRadius = 5
    return cell
}

但是我有很多错误,我不知道如何改正它们。

最佳答案

您可以尝试为每个 collectionView 分配一个唯一的标签,如下所示

collectionView.tag = 1

然后使用标签作为标识符:

switch collectionView.tag {
    case 0:
        // some code
    case 1:
        // some code
    default:
        // some code
}

如果您在 UITableViewCell 中使用您的 collectionView,您可以将每个 collectionView.tag 设置为 indexPath.section tableView(_ tableView: willDisplay cell: forRowAt indexPath:) 中的 indexPath.row

这是您可以使用的一个不错的子类:

class CollectionViewTableViewCell: UITableViewCell {
    @IBOutlet weak var collectionView: UICollectionView!

    func setCollectionViewDataSourceDelegate(dataSource: UICollectionViewDataSource?, dataDelegate: UICollectionViewDelegate?, forSection section: Int) {

        collectionView.delegate = dataSource as! UICollectionViewDelegate?
        collectionView.dataSource = dataDelegate as! UICollectionViewDataSource?
        collectionView.tag = section
        collectionView.reloadData()
    }
}

然后在你的 View Controller 中实现

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    guard let tableViewCell = cell as? CollectionViewTableViewCell else { return }
    tableViewCell.setCollectionViewDataSourceDelegate(dataSource: self, dataDelegate: self, forSection: indexPath.section)
}

关于swift - 如何在 Swift 中使用 switch 语句实现 collectionView 功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42347338/

相关文章:

iphone - UICollectionView 在不同的批量更新中累积动画

ios - 向上/向下滚动时 UICollectionViewCell 内容发生变化

ios - 如何自定义单元格的一部分

swift - 不正确的字符串到日期转换 swift 3.0

swift - 即使在将数据设置为 Swift 4 后,Collection View 数据也不会更新

ios - 在 UICollectionView 中禁用 UIAttachmnetBehavior 的垂直移动

ios - 我的 CollectionViewCell 陷入困惑

ios - SwiftUI DispatchQueue 按钮

ios - 添加 uiview 作为背景 View

ios - NSFetchedResultsController 与像 iMessage 的表