gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:57:01 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com