swift - 一个 View Controller 中的两个 TableView swift2.0 中带有 didSelectRowAtIndexPath

标签 swift uitableview didselectrowatindexpath

我在一个 View Controller 中有两个 TableView 。我们假设表为 table1 和 table2。当我在 table1 中选择一行时,我想更改 table2 中的数据。我怎么做? (我不能使用 segue 标识符,因为这些表在一​​个窗口中)

我的代码如下。在我的表 1 中,我有 lessons1 数组。在表 2 中,我有 Lessons2 数组。所以现在我想单击 table1 的第二行并在 table2.likewise 的资源数组中显示数据。你能帮我写代码吗?我试过如下。但我知道这是完全错误的

 let Resources = ["Word Doc on Probability", "Extra Sums", "Homework","",""]

 let lessons1 = ["Term1", "Term2"," Term3"]

 let Lessons2 = ["Create a set theory", "Draw Ven Diagrams", "iOS Developer Tips"]

  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of items in the sample data structure.

var count:Int?

if tableView.tag == 3 {
    count = lessons1.count
}
 else if tableView.tag == 4 {
    count = Lessons2.count
} return count!


}
       func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

var cell:UITableViewCell?

if tableView.tag == 3 {
    cell = tableView.dequeueReusableCellWithIdentifier("TextCell3", forIndexPath: indexPath)

    let row = indexPath.row
    cell!.textLabel!.text = lessons1[row]

}

 else if tableView.tag == 4 {
    cell = tableView.dequeueReusableCellWithIdentifier("TextCell4", forIndexPath: indexPath)

    let row = indexPath.row
    cell!.textLabel!.text = Lessons2[row]

}

return cell!
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
  ////this code is wrong/////
let row = indexPath.row
if tableView.tag == 3 {
    if indexPath.row == 1 {
        let cell = tableView.dequeueReusableCellWithIdentifier("TextCell4", forIndexPath: indexPath)


        cell.textLabel!.text = Objectives[row]
        lessons.reloadData()

    }

}

最佳答案

tableView 反射(reflect)存储在模型中的数据。当在 table1 中选择一行时,更新 table2 模型中的数据,然后调用 table2.reloadData()

@IBOutlet tableView1: UITableView!
@IBOutlet tableView2: UITableView!

let Resources = ["Word Doc on Probability", "Extra Sums", "Homework","",""]

let lessons1 = ["Term1", "Term2"," Term3"]

let Lessons2 = ["Create a set theory", "Draw Ven Diagrams", "iOS Developer Tips"]

var dataForTable1 = [String]()
var dataForTable2 = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    dataForTable1 = lessons1
    dataForTable2 = Lessons2
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // Return the number of items in the sample data structure.

    var count = 0

    if tableView.tag == 3 {
        count = dataForTable1.count
    }
    else if tableView.tag == 4 {
        count = dataForTable2.count
    }

    return count

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell:UITableViewCell?

    if tableView.tag == 3 {
        cell = tableView.dequeueReusableCellWithIdentifier("TextCell3", forIndexPath: indexPath)

        let row = indexPath.row
        cell!.textLabel!.text = dataForTable1[row]

    }

    else if tableView.tag == 4 {
        cell = tableView.dequeueReusableCellWithIdentifier("TextCell4", forIndexPath: indexPath)

        let row = indexPath.row
        cell!.textLabel!.text = dataForTable2[row]

    }

    return cell!
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    let row = indexPath.row
    if tableView.tag == 3 {
        if indexPath.row == 1 {
            dataForTable2 = Resources
            tableView2.reloadData()

        }
    }
}

关于swift - 一个 View Controller 中的两个 TableView swift2.0 中带有 didSelectRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40478555/

相关文章:

ios - 无法将删除 subview 置于前面

objective-c - 当我点击 didSelectRowAtIndexPath 时,UISearchBar 中的自动更正会干扰

ios - 从 UITableViewCell 转入如何包含 indexPath 数据?

ios - 选择表格中的单元格无法正常工作

ios - 在 iOS 中,使用 Swift,我想显示用户的位置,但是蓝点没有出现在 MapView 上

iOS 日历 View - 来自 JSON 的提要,显示详细信息

ios - 快速触摸开始并将其显示在触摸的对象上

javascript - 解析服务器云代码获取和更新对象

Swift - 从自定义单元和单元数据传递到另一个 Controller

ios - 在 Swift 中选择行时删除标签背景颜色