如何从表静态单元格更改按钮标题?

时间:2017-03-12 21:19:18

标签: ios swift xcode swift3 ios10

enter image description here

我有一个HomeViewController,它以模块化的形式进入一个标识为: pickSubjectAction

的导航控制器

在SubjectPickerTableViewController上我的主题可供选择。这是我的代码

import UIKit


class SubjectPickerTableViewController: UITableViewController {

    var subjects:[String] = [
        "English",
        "Math",
        "Science",
        "Geology",
        "Physics",
        "History"]

    var selectedSubject:String? {
        didSet {
            if let subject = selectedSubject {
                selectedSubjectIndex = subjects.index(of: subject)!
            }
        }
    }
    var selectedSubjectIndex:Int?

    override func viewDidLoad() {
        super.viewDidLoad()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

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

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


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SubCell", for: indexPath)
        cell.textLabel?.text = subjects[indexPath.row]

        if indexPath.row == selectedSubjectIndex {
            cell.accessoryType = .checkmark
        } else {
            cell.accessoryType = .none
        }
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

        //Other row is selected - need to deselect it
        if let index = selectedSubjectIndex {
            let cell = tableView.cellForRow(at: IndexPath(row: index, section: 0))
            cell?.accessoryType = .none
        }

        selectedSubject = subjects[indexPath.row]

        //update the checkmark for the current row
        let cell = tableView.cellForRow(at: indexPath)
        cell?.accessoryType = .checkmark
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "saveSelectedSubject" {
            if let cell = sender as? UITableViewCell {
                let indexPath = tableView.indexPath(for: cell)
                if let index = indexPath?.row {
                    selectedSubject = subjects[index]
                }
            }
        }
    }
}

这也是标识符: savedSelectedSubject

Q1:我怎么能从按钮到tableview控制器? 我试过这个却失败了 enter image description here Q2:如何更改所选主题标题的按钮?

我的资源:https://www.raywenderlich.com/113394/storyboards-tutorial-in-ios-9-part-2

感谢任何帮助。感谢

1 个答案:

答案 0 :(得分:0)

  

我怎样才能从按钮到tableview控制器?我试过了   这但是失败了

performSegueWithIdentifierString格式发送时需要Any格式。调用此函数的正确方法是

self.performSegue(withIdentifier: "pickSubjectAction", sender: self)
  

如何更改所选主题标题的按钮?

我想你想在 savedSelectedSubject segue之后的控制器中反映所选主题。让ViewController A成为您推送/呈现的一个,ViewController B是推送/呈现的那个。请按照以下步骤操作:

  1. 为此,您需要通过prepare(for segue: UIStoryboardSegue, sender: Any?)属性从segue.destination函数中获取目标控制器(B)
  2. 在B中创建一个公共变量,您可以在其中设置所选主题。 使用该属性,您可以显示您选择的主题标题。

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      let B = segue.destination
        // Assuming B's public var is named selectedSubject and is of String type
       if let subject = self.selectedSubject {
          B.selectedSubject = subject
        }
    }
    
  3. 在控制器B中

    override func viewWillAppear() {
      super.viewWillAppear()
      button.setTitle(self.selectedSubject, for: .normal)
    }
    // We already have the value of selectedSubject which we are using to set title for the button.