2016-11-08 31 views
0

我在一個視圖控制器中有兩個表視圖。我們假設表格爲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

回答

0

一個的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

0
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 
     } 
    } 
} 
相關問題