ios - 如何在 UITableview 中使用两个自定义单元格

标签 ios xcode uitableview cocoa-touch swift4

var mutipleArrayForFirstCell:[String] = ["1","2","3","4","5","6","7","8","9","10"]
var mutipleArrayForSecondCell:[String] = ["11","12","13","14","15","16","17","18","19","20"]

//MARK - TableView delegates
func numberOfSections(in tableView: UITableView) -> Int {
    return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 1 {
        return mutipleArrayForFirstCell.count
    }else {
        return mutipleArrayForSecondCell.count
    }  
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if indexPath.row == 0 {
        let valueAt1stCell = tableView.dequeueReusableCell(withIdentifier: "FirstCell", for: indexPath) as! multipleCustomCellTableViewCell
        valueAt1stCell.Firstlabel.text =  mutipleArrayForFirstCell[indexPath.row]

        return valueAt1stCell

    } else {
        let valueAt2ndCell = tableView.dequeueReusableCell(withIdentifier: "SecondCell", for: indexPath) as! SecondCustomTableviewCell
        valueAt2ndCell.secondLabel.text = mutipleArrayForSecondCell[indexPath.row]
        return valueAt2ndCell
    }

}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

我的问题是我想在两个不同的单元格中显示上面声明的两个数组值。我该怎么做??它显示如下:

enter image description here

但它应该在一个单元格中显示 1-10,在另一个单元格中显示 11-20。有人请帮助我,在此先感谢

enter image description here

我怎样才能在两个单元格之间留出一些空间?

最佳答案

试试这个

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

switch indexPath.section {
    case 0:
        //First Array
      let valueAt1stCell = tableView.dequeueReusableCell(withIdentifier: "FirstCell", for: indexPath) as! multipleCustomCellTableViewCell
    valueAt1stCell.Firstlabel.text =  mutipleArrayForFirstCell[indexPath.row]

    return valueAt1stCell

    default:
        //Second Array
     let valueAt2ndCell = tableView.dequeueReusableCell(withIdentifier: "SecondCell", for: indexPath) as! SecondCustomTableviewCell
    valueAt2ndCell.secondLabel.text = mutipleArrayForSecondCell[indexPath.row]
    return valueAt2ndCell

    } 
}

关于ios - 如何在 UITableview 中使用两个自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51277699/

相关文章:

objective-c - 需要帮助舍入数字( float )

ios - 自动单词生成器

ios - UICollectionViewCell 在 UICollectionViewReusableView 宽度之后启动

ios - 关于自定义 tableView 单元格中内存泄漏的基本建议

ios - Ionic 1 应用程序 XHR 请求在浏览器/Android 上工作在 iOS 上失败

ios - 什么都不显示时如何在 Xcode 中自动隐藏 Collection View ?

ios - 如何在 Uber 等 iOS 设备上跟踪 GPS 位置以进行充电?

objective-c - 更新 NSArray 上的值

ios - 如何在 UIPicker 中首先选择具有特定标题的数组

iphone - UITableView rowHeight 空中变化