如何在Swift中使用switch语句实现collectionView函数?

时间:2017-02-20 14:37:42

标签: 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
}

但我犯了很多错误,我不知道如何解决它们。

2 个答案:

答案 0 :(得分:2)

您可以尝试为每个collectionView分配一个如此唯一的标记

collectionView.tag = 1

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

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

如果您在UITableViewCell内使用了collectionViews,则可以在collectionView.tag内将每个indexPath.section设置为indexPath.rowtableView(_ tableView: willDisplay cell: forRowAt indexPath:)

这是一个很好用的子类:

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()
    }
}

然后在视图控制器实现中

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)
}

答案 1 :(得分:0)

希望这有帮助。根本不测试。如果有任何问题,你能不能解决一下?

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

}