gpt4 book ai didi

ios - 如何使用 swift3 在​​ UITableView 的两个不同单元格中显示两个数组

转载 作者:行者123 更新时间:2023-11-28 18:50:28 26 4
gpt4 key购买 nike

我试图在表格 View 中的两个不同单元格中显示两个数组,但它不起作用,任何人都可以帮助我解决这个问题,我正在使用 Xcode8 和 Swift3,这是我的 TableView 代码。

这就是我想要显示两个数组的方式:

行 1 = aaa

第 2 行 = 11111

第 3 行 = bbb

第 4 行 = 2222

第 5 行 = cccc

第 6 行 = 3333

第 7 行 = ddd

第 8 行 = eee

第 9 行 = fff

我的代码:

 import UIKit

class _CellsTableViewController: UITableViewController {

var array1 = [String]()
var array2 = [String]()

override func viewDidLoad() {
super.viewDidLoad()

array1 = ["aaa","bbb","ccc","ddd","eee","fff"]

array2 = ["11111","22222","33333"]
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {



return array1.count + array2.count


}


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

if indexPath.row == 0 {

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

cell.textLabel?.text = array1[indexPath.row]

return cell

}
else {

let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath)

cell.textLabel?.text = array2[indexPath.row]

return cell

}

}


}

最佳答案

将两个数组组合成一个数组,然后重新加载表。看下面的代码,

let array1 = ["aaa","bbb","ccc","ddd","eee","fff"]
let array2 = ["11111","22222","33333"]
var arrayAllItems = [String]()
for i in 0..<max(array1.count, array2.count){
if array1.count > i{
arrayAllItems.append(array1[i])
}
if array2.count > i{
arrayAllItems.append(array2[i])
}
}

用数组 arrayAllItems 重新加载表

关于ios - 如何使用 swift3 在​​ UITableView 的两个不同单元格中显示两个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42388772/

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