表格视图单元格的外壳。迅速

千倍体

我有一个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

Ashwini Salunkhe

您可以使用切换用例来做到这一点,如下所示:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章