将按钮的状态保存在一个动态表查看单元格中,以在另一个uitableviewController中查看



这是场景。我有一个带有动态单元格的UitaiteViewController。每个单元格都有3个按钮,用户可以单击这些按钮。在应用程序中的另一点我需要用户查看另一个无法编辑的uitaiteViewController,并且在按钮中与用户在第一个UitaiteViewController中单击后看到的按钮具有相同的状态。

换句话说,这是一个历史页面。目前,第一个UitaiteViewController具有UITATION VIEWCONTROLLERDATASOURCE,并且我尝试仅在第二个表视图控制器上使用相同的数据源,但是由于某种原因,它无法正常工作。当用户在应用程序上向后导航以查看他们最初进行选择的第一个UaithitViewController时,它可以保存状态,但是当它们到达应用程序的末尾时,新的UaithitviewController具有所有新的(未选择(的状态。那些按钮。

我还需要第二个UitaiteViewController具有不同的颜色和其他一些小差异,但是目前,我最担心的是确保可以从较早的状态访问这些状态。

这是我的数据源:

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

}

您拥有正确的一般想法。表视图是一个视图对象,它显示有关模型对象的信息,并收集用户的输入。您想将该输入保存回模型。

对于非剖面的表视图,通常您需要将结构数组作为数据模型。您可以将该数组传递到需要显示模型信息的任何视图控制器。您可以将该方法用于主段,其中用户点击单元格以获取有关该项目的详细信息(只需将所选单元格的索引传递给详细信息视图控制器。(

在您的情况下,您想在2个位置显示数组的所有内容,因此您将按钮状态保存到数组中的条目中,然后在两个视图控制器中显示这些状态。

如果它不起作用,则需要编辑您的帖子以显示您的代码。显示数据模型。展示如何在视图控制器之间传递。显示如何将信息从模型安装到表视图中(通过发布表查看数据源方法 - 尤其是cellForRow(at:)

最新更新