如何使用 switch 语句在 Swift 中实现 collectionView 函数? [英] How can I use switch statement to implement collectionView function in Swift?

查看:29
本文介绍了如何使用 switch 语句在 Swift 中实现 collectionView 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 swift 的新手,并试图在 ViewController 中实现 collectionView 功能.现在我有三种类型的 UICollectionViewCell,这是我的代码:

I am new to swift and trying to implement collectionView function in a ViewController.Now I have three types of UICollectionViewCell,here is my code:

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语句达到同样的效果吗?让代码看起来更优雅一点?

Can I use switch statement to achieve the same effect?Make the code look a little more elegant?

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

I hope my code looks like this:

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
}

但是我犯了很多错误,我不知道如何改正.

But I got a lot of mistakes, and I do not know how to fix them.

推荐答案

希望对您有所帮助.根本不测试.如果有任何问题,您可以剪一下吗?

Hope this help. Not testing at all. If any issues, can you take a snip ?

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

  let cell:UICollectionViewCell

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

}

这篇关于如何使用 switch 语句在 Swift 中实现 collectionView 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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