【问题标题】:How to use two custom cell in UITableview如何在 UITableview 中使用两个自定义单元格
【发布时间】:2018-12-19 00:51:13
【问题描述】:
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
}

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

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

如何在两个单元格之间留出一些空间?

【问题讨论】:

  • 只需将indexPath.row 中的indexPath.section 更改为cellForRowAt
  • IndexPath 具有节和行值。使用 indexPath.section 值来分隔内容而不是 indexPath.row
  • 是的,现在它正在工作@Pratik Prajapati

标签: ios xcode uitableview cocoa-touch swift4


【解决方案1】:

试试这个

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

    } 
}

【讨论】:

    【解决方案2】:
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {        
        if indexPath.section == 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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-27
      • 1970-01-01
      • 2013-01-04
      相关资源
      最近更新 更多