簡體   English   中英

如何使用swift3在UITableView中的兩個不同單元格中顯示兩個數組

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM