Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 表视图单元格的大小写。敏捷的_Ios_Swift_Uitableview_Uiview_Uicollectionview - Fatal编程技术网

Ios 表视图单元格的大小写。敏捷的

Ios 表视图单元格的大小写。敏捷的,ios,swift,uitableview,uiview,uicollectionview,Ios,Swift,Uitableview,Uiview,Uicollectionview,我有一个tableview,我制作了cell.xib。 在这些tableview中,将有不同的cells,我对xib也会这样做 我如何使用它的一个例子 if indexPath.row == 0 { let cell = tableView.dequeueReusableCell( withIdentifier: TableViewCellOne.identifier, for: indexPath) as? TableViewCellOne

我有一个
tableview
,我制作了
cell.xib
。 在这些
tableview
中,将有不同的
cell
s,我对
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
实现这一点?

您可以像这样编写协议
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()
}

您可以这样编写协议
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()
}

您可以按如下方式使用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()
       }
}

您可以按如下方式使用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()
       }
}