swift2.0中使用didSelectRowAtIndexPath的swift中的一个视图控制器中的两个表视图

问题描述:

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

我的代码如下。并在我的表1中我有lesson1数组。在表2中,我有Lessons2数组。所以现在我想单击table1的第二行,并在table2中的资源数组中显示数据。 u能请帮助我的code.I尝试作为follows.But我知道这是完全错误的

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() 

    } 

} 
+0

有你在** super.viewDidLoad()设置标签**? – iDeveloper

+0

** table1.tag = 1 **和** table2.tag = 2 ** – iDeveloper

+0

是的在故事板我设置 – Emily

一个的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() 

     } 
    } 
} 
+0

我该怎么做? – Emily

+0

你需要给出一个更好的例子,说明你的两个表格正在显示什么。例如,如果您有单词列表,则第一个表格可能是字母表中的字母,第二个表格可能是以该字母开头的所有单词。在'didSelectRowAtIndexPath'中,您将得到与表1行对应的字母,然后过滤单词数组以创建将在表2中显示的数组。然后,您将调用'table2.reloadData()'和它会使用该数组来显示单词。 – vacawama

+0

看到我的代码如下 – Emily

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