2017-02-22 3 views
0

Я пытаюсь отобразить два массива в двух разных ячейках в виде таблицы, но это не работает, может ли кто-нибудь помочь мне решить эту проблему, я используя Xcode8 и Swift3, и это мой код TableView.Как отобразить два массива в двух разных ячейках в UITableView с помощью swift3

это, как я хочу, чтобы отобразить два массива:

Row1 = ааа

row2 = 11111

row3 = БББ

row4 = 2222

row5 = КЦИК

ряд6 = 3333

row7 = ддд

row8 = еее

row9 = 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 

    } 

} 


} 
+0

, что проблема у столкнулась –

+0

Фатальная ошибка: индекс вне диапазона, то будет ошибка. –

+1

Почему, черт возьми, вы думаете, 'если indexPath.row == 0' сделал бы это? Кажется основной проблемой программирования. –

ответ

1

Я не понимаю, почему вы должны это сделать. Но вы код должен быть:

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

} 

Но я не проверяю этот код.

+0

Спасибо, что Сергей работает. –

3

сделать один массив с комбинацией двух, а затем перезагрузить таблицу. Смотрите следующий код,

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 таблица с массивом arrayAllItems

+0

это намного лучше и чище, чем принятый ответ. – zero3nna

+0

Спасибо, что тоже работает. –

Смежные вопросы