2017-04-20 55 views
-1

以下是該場景。我有一個帶動態單元的UITableViewController。每個單元格都有3個用戶可以點擊的按鈕。在應用程序的另一個地方,我需要用戶看到另一個不可編輯的UITableViewController,並且在用戶看完第一個UITableViewController後單擊它時看到的按鈕具有相同的狀態。將動態表格視圖中按鈕的狀態保存在一個UITableViewController中以在另一箇中查看

換句話說,這是一個歷史頁面。現在第一個UITableViewController有一個UITableViewControllerDataSource,並且我試過對第二個Table View Controller使用相同的數據源,但由於某種原因它不起作用。當用戶在應用程序上向後導航以查看他們最初選擇的第一個UITableViewController時,它將狀態保存得很好,但是當它們到達應用程序的末尾時,新的UITableViewController具有所有新(未選定)狀態那些按鈕。

我還需要第二次的UITableViewController有不同的顏色和其他一些小的差異爲第一,但現在我最關心的是確保我可以從前面訪問這些國家。

這裏是我的數據源:

class DepressedButtonTableViewControllerDataSource: NSObject, UITableViewDataSource, DepressedSelectionTableViewCellDelegate { 

//MARK: Array for putting in my selections, and some other important variabls for managing choices 
var choices: Array<Int> = [] 
var result = 0 
var advanceArray: Array<Int> = [] 

// MARK: - Table view data source 

func numberOfSections(in tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return 3 
} 


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


    let cell = tableView.dequeueReusableCell(withIdentifier: "depressedSelection", for: indexPath) as! DepressedSelectionTableViewCell 

    let firstThing = "placeholder" 
    let secondThing = "other texxt" 
    let thirdThing = "multiple lines! look ma! look at this!" 

    let cellData = [firstThing, secondThing, thirdThing] 
    let text = cellData[indexPath.row] 

    cell.cellText.text = text 


    //configure muh buttons 
    cell.yes.setImage(#imageLiteral(resourceName: "yes_unselected"), for: .normal) 
    cell.yes.setImage(#imageLiteral(resourceName: "yes_selected"), for: .selected) 
    cell.no.setImage(#imageLiteral(resourceName: "no_unselected"), for: .normal) 
    cell.no.setImage(#imageLiteral(resourceName: "no_selected"), for: .selected) 
    cell.na.setImage(#imageLiteral(resourceName: "na_unselected"), for: .normal) 
    cell.na.setImage(#imageLiteral(resourceName: "na_selected"), for: .selected) 

    cell.delegate = self 


    return cell 
} 

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    return 100 
} 

//delegate code 

func filledOut(controller: DepressedSelectionTableViewCell, selections: Int!, isClicked: Bool!) { 
    choices.append(selections) 
    if isClicked == true { 

     advanceArray.append(1) 
    } 
    if advanceArray.count == 3 { 
     print(choices.reduce(0, +)) 
     result = (choices.reduce(0, +)) 
     print(advanceArray.count) 
     NotificationCenter.default.post(name: Notification.Name("TableViewToDepressedParent"), object: nil) 
    } 

} 

}

+1

請編輯您的問題,包括相關的代碼 – Paulw11

+0

謝謝你,我已經包含了相關的代碼 –

回答

1

你已經得到了正確的總體思路。表視圖是一個視圖對象,顯示有關模型對象的信息並收集用戶的輸入。您想要將該輸入保存回模型中。

對於非分段表視圖結構的數組往往是你所需要的數據模型。您可以將該數組傳遞給任何需要顯示模型信息的視圖控制器。你可以使用這種方法來獲取主細節,用戶在細胞上點擊以轉到有關該項目的詳細信息(只需將選定單元格的索引傳遞給細節視圖控制器即可。)

在您的情況下,您希望將數組的所有內容顯示在2個位置,這樣可以將按鈕狀態保存到數組中的條目中,然後在兩個視圖控制器中顯示這些狀態。

如果它不工作,你將需要編輯您的文章,以顯示你的代碼。顯示數據模型。顯示它如何在視圖控制器之間傳遞。顯示你如何安裝與您的機型信息到你的表視圖(由發佈您的表視圖數據源的方法 - 尤其是cellForRow(at:)

+0

謝謝。您的投入。我已經添加了相關的代碼。我覺得我接近理解你在說什麼,但我不知道我會創建陣列。這純粹是修改一個變量數組當用戶點擊給定單元格中的按鈕時? –

相關問題