![](/img/trans.png)
[英]How to display two different cells in UICollectionView in swift3
[英]How to display two arrays in two different cells in UITableView using swift3
我试图在一个表视图中的两个不同的单元格中显示两个数组,但是它不起作用,有谁能帮助我解决这个问题,我正在使用Xcode8和Swift3,这是我的TableView代码。
这就是我要显示两个数组的方式:
第1行= aaa
第2行= 11111
row3 = bbb
第4行= 2222
第5行= cccc
第6行= 3333
row7 = 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
重新加载表
我不明白您为什么需要这样做。 但是您的代码应该是:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var index = indexPath.row/2;
if(index<array1.count %% index<array2.count)
{
if (indexPath.row % 2 == 0){
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = array1[index]
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath)
cell.textLabel?.text = array2[index]
return cell
}
}
else {
var isFirst
if(index>=array1.count)
{
index = indexPath.row - array1.count;
isFirst = false
}else
{
index = indexPath.row - array2.count;
isFirst = true
}
if(isFirst)
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = array1[index]
return cell
}else
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath)
cell.textLabel?.text = array2[index]
return cell
}
}
}
但是我不测试这段代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.