0

I am trying to display two arrays in two different cells in a table view but it doesn't works , can any one help me to solve this problem , I am using Xcode8 and Swift3 , and this is my TableView code .

this is how I want to display the two arrays :

row1 = aaa

row2 = 11111

row3 = bbb

row4 = 2222

row5 = cccc

row6 = 3333

row7 = ddd

row8 = eee

row9 = fff

My code :

 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

    }

 }


 }
8
  • what the problem u faced Commented Feb 22, 2017 at 10:39
  • fatal error: Index out of range , thats the error. Commented Feb 22, 2017 at 10:40
  • 1
    Why the heck do you think if indexPath.row == 0 would do this? Seems a basic programming issue. Commented Feb 22, 2017 at 10:40
  • Ok, would you show me how to solve it . Commented Feb 22, 2017 at 10:41
  • ya surely you get , bz if you are using if indexPath.row == 0 { , it is the one index and at the same time your array count is 9 but you load different ok simple this is if indexPath.row == 0 { or if indexPath.section == 0 { Commented Feb 22, 2017 at 10:41

2 Answers 2

3

make a single array with combination of two and then reload table. See the following code,

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])
    }
}

Reload table with array arrayAllItems

Sign up to request clarification or add additional context in comments.

1 Comment

this is much better and cleaner then the accepted answer.
1

I don't understand why you need to do this. But you code should be:

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
        }
    }

 }

But I don't test this code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.