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

标签 ios uitableview swift3

我试图在表格 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/

相关文章:

ios - 当按下回车键时,UITableView 中的 UITextView 不会向上滚动

objective-c - 编辑 UITableView 时如何防止删除按钮弹出?

ios - 如何使用 swift3 从 firebase 数据库中存储对象有多个字符串

ios - UITextView 在行尾点击不应返回下一行中的单词

android - 将 Windows C++ 用于 Android 和 iOS

ios - Swift 4 从 UITableView 执行 segue 并传递数据

ios - iphone X 最大加速度计和陀螺仪频率

uitableview - 使用 Storyboard的一个 UITableviewcontroller 中的静态和动态单元格

ios - 使用超链接集成 UPI 后如何获得响应

json - 使用 Alamofire 获取数组键值