【问题标题】:Case for table view cell. swift表格视图单元格的案例。迅速
【发布时间】:2020-09-05 11:58:36
【问题描述】:

我有一个tableview,我创建了cell.xib。 在这些tableview 中会有不同的cells,我对xib 做同样的事情。

我如何使用它的一个例子。

 if indexPath.row == 0 {
         let cell = tableView.dequeueReusableCell( withIdentifier: TableViewCellOne.identifier,
            for: indexPath) as? TableViewCellOne

        return cell ?? UITableViewCell()
        }

if indexPath.row == 1 {
         let cell = tableView.dequeueReusableCell( withIdentifier: TableViewCellTwo.identifier,
            for: indexPath) as? TableViewCellTWo

        return cell ?? UITableViewCell()
        }
              return UITableViewCell()
        }

但我不喜欢这种方法。我如何使用case 做到这一点?

【问题讨论】:

    标签: ios swift uitableview uiview uicollectionview


    【解决方案1】:

    你可以像这样编写协议DequeueInitializable及其扩展

    protocol DequeueInitializable {
        static var reuseableIdentifier: String { get }
    }
    
    extension DequeueInitializable where Self: UITableViewCell {
    
        static var reuseableIdentifier: String {
            return String(describing: Self.self)
        }
    
        static func dequeue(tableView: UITableView) -> Self {
            guard let cell = tableView.dequeueReusableCell(withIdentifier: self.reuseableIdentifier) else {
                return UITableViewCell() as! Self
            }
            return cell as! Self
        }
        static func register(tableView: UITableView)  {
            let cell = UINib(nibName: self.reuseableIdentifier, bundle: nil)
            tableView.register(cell, forCellReuseIdentifier: self.reuseableIdentifier)
        }
    }
    
    extension DequeueInitializable where Self: UICollectionViewCell {
    
        static var reuseableIdentifier: String {
            return String(describing: Self.self)
        }
    
        static func dequeue(collectionView: UICollectionView,indexPath: IndexPath) -> Self {
    
           let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.reuseableIdentifier, for: indexPath)
    
            return cell as! Self
        }
         static func register(collectionView: UICollectionView)  {
              let cell = UINib(nibName: self.reuseableIdentifier, bundle: nil)
              collectionView.register(cell, forCellWithReuseIdentifier: self.reuseableIdentifier)
          }
    }
    

    然后使用此协议确认您的单元

    class TableViewCellOne: UITableViewCell, DequeueInitializable {
    }
    

    然后在你的cellForRow 方法中

    switch (indexPath.row) {
       case 0:
        return TableViewCellOne.dequeue(tableView: tableView)
       case 1:
        return TableViewCellTwo.dequeue(tableView: tableView)
       default:
       return UITableViewCell()
    }
    

    【讨论】:

      【解决方案2】:

      你可以使用 switch case 来做到这一点,如下所示:

      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
              switch section {
              case 0: //TableViewCellOne
                      return 4
              case 1: //TableViewCellTwo                
                      return 5                
              default:
                      return 0
              }
          }
      

      那么cellforRow 方法将是:

       func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      
      switch indexPath.section {
           case 0:  //cell One
                  let cell = tableView.dequeueReusableCell( withIdentifier: TableViewCellOne.identifier,
                      for: indexPath) as? TableViewCellOne    
                  return cell ?? UITableViewCell()
      
           case 1: //cell two
                  let cell = tableView.dequeueReusableCell( withIdentifier: TableViewCellTwo.identifier,
                      for: indexPath) as? TableViewCellTWo    
                  return cell ?? UITableViewCell()
      
           default: 
                   return UITableViewCell()
             }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-03-10
        • 1970-01-01
        • 2019-04-18
        • 1970-01-01
        • 2015-09-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多