表格视图单元格的情况。迅速

问题描述 投票:1回答:2

我有一个tableview,我取得了cell.xib。在这些tableview中,将使用与cell相同的不同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 view case
2个回答
2
投票

您可以通过如下使用开关盒来做到这一点:

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

3
投票

您可以像这样编写协议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()
}
© www.soinside.com 2019 - 2024. All rights reserved.