【问题标题】:Two table views in one view controller swift with didSelectRowAtIndexPath in swift2.0swift2.0中的didSelectRowAtIndexPath在一个视图控制器中的两个表视图
【发布时间】:2016-11-08 03:40:14
【问题描述】:

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

我的代码如下。在我的表 1 中,我有课程 1 数组。在表 2 中,我有课程 2 数组。所以现在我想点击 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()

    }

}

【问题讨论】:

  • 你在 super.viewDidLoad() 中设置标签了吗?
  • table1.tag = 1table2.tag = 2
  • 是的,在我设置的情节提要中

标签: swift uitableview didselectrowatindexpath


【解决方案1】:

tableView 反映了存储在模型中的数据。当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()

        }
    }
}

【讨论】:

  • 我该怎么做?
  • 您需要提供一个更好的示例来说明您的两个表格所显示的内容。例如,如果您有一个单词列表,您的第一个表可能是字母表中的字母,第二个表可能是所有以该字母开头的单词。在didSelectRowAtIndexPath 中,您将获得对应于表一的行的字母,然后过滤单词数组以创建将显示在表 2 中的数组。然后您将调用 table2.reloadData() 并使用它数组来显示单词。
【解决方案2】:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    if tableView.tag == 3 
    {
        if indexPath.row == 1 
        {
            Lessons2 = Resources
            lessons.reloadData() //table2 is data reload
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多