Arrays 在UITableview swift中使用两个数组

Arrays 在UITableview swift中使用两个数组,arrays,swift,uitableview,Arrays,Swift,Uitableview,我在一个单元格中有两个标签,在一个表视图中有两个数组,我想用标签链接每个数组 具有la_视图的列表[]和具有la_视图的列表2[], 在表视图中的一个单元格中也有la_视图和la_视图2 运行程序时显示错误,如图所示 var list = [String]() var list_2 = [String]() func tableView (_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

我在一个单元格中有两个标签,在一个表视图中有两个数组,我想用标签链接每个数组

具有la_视图的列表[]和具有la_视图的列表2[], 在表视图中的一个单元格中也有la_视图和la_视图2

运行程序时显示错误,如图所示

var list = [String]()
var list_2 = [String]()

func tableView (_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

      return list.count + list_2.count

}

func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell_1") as! TableView_Cell
        print("\(list.count)")
        cell.la_view.text = list[indexPath.row]
        cell.la_view2.text = list_2[indexPath.row] // eroor here
        cell.backgroundColor = UIColor(named: "Defeult")
        return cell
}

// This append in arrays
func append(add:Int) {
    list.append("\(add)")
    list_2.append("\(add)")
    let indexPath = IndexPath(row: list.count - 1, section: 0)
    let indexPath2 = IndexPath(row: list_2.count - 1, section: 0)
    table_View.beginUpdates()
    table_View.insertRows(at: [indexPath], with: .automatic)
    table_View.insertRows(at: [indexPath2], with: .automatic)
    table_View.endUpdates()
}

不要那样做。不要将多个数组用作数据源

return list.count + list_2.count
导致错误,因为实际上您只有
list.count
list.count中的项目数必须等于
list\u 2.count
。此添加在第行
list.count+1

使用自定义结构

struct Item {
    let foo : String
    let bar : String
}
然后映射这两个数组

var items = [Item]()

items = zip(list, list_2).map{ Item(foo:$0.0, bar:$0.1) }
numberofrowsin部分
return
items.count中

cellForRowAt
中,从
实例获取值

let item = items[indexPath.row]
cell.la_view.text = item.foo
cell.la_view2.text = item.bar
要附加项,请使用

func append(add:Int) {
    let lastIndex = items.count
    items.append( Item(foo:"\(add)", bar:"\(add)") )
    let indexPath = IndexPath(row: lastIndex, section: 0)
    table_View.insertRows(at: [indexPath], with: .automatic)
}

请按照命名约定使用小写而不是小写的变量名。

不要这样做。不要将多个数组用作数据源

return list.count + list_2.count
导致错误,因为实际上您只有
list.count
list.count中的项目数必须等于
list\u 2.count
。此添加在第行
list.count+1

使用自定义结构

struct Item {
    let foo : String
    let bar : String
}
然后映射这两个数组

var items = [Item]()

items = zip(list, list_2).map{ Item(foo:$0.0, bar:$0.1) }
numberofrowsin部分
return
items.count中

cellForRowAt
中,从
实例获取值

let item = items[indexPath.row]
cell.la_view.text = item.foo
cell.la_view2.text = item.bar
要附加项,请使用

func append(add:Int) {
    let lastIndex = items.count
    items.append( Item(foo:"\(add)", bar:"\(add)") )
    let indexPath = IndexPath(row: lastIndex, section: 0)
    table_View.insertRows(at: [indexPath], with: .automatic)
}

请根据命名约定使用小写而不是蛇壳变量名。

有什么错误?有什么错误?然后映射两个数组,在哪里写这个?这只是一个简单组合数组的示例。您应该通过直接创建
Item
实例来填充数据源数组。
Item
也是一个例子。使用一个更有意义的名称。我打算建议使用一个tulples数组来快速解决问题,但如上所述,定制对象是最好的选择。tableView中的多个数组只是询问问题,以及indexpath超出范围的错误。然后映射这两个数组,在哪里编写?这只是一个简单组合数组的示例。您应该通过直接创建
Item
实例来填充数据源数组。
Item
也是一个例子。使用一个更有意义的名称。我打算建议使用一个tulples数组来快速解决问题,但如上所述,定制对象是最好的选择。tableView中的多个数组只是询问问题,以及indexpath超出范围的错误。