ios - 如何更改表格静态单元格的按钮标题?

标签 ios swift xcode swift3 ios10

enter image description here

我有 HomeViewController,它以模态方式连接到导航 Controller ,其标识符为: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 Controller ? 我尝试过但失败了 enter image description here Q2:如何更改所选主题的按钮标题?

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

感谢任何帮助。谢谢

最佳答案

How can i segued from button to the tableview controller? I tried this but failed

当您以 Any 格式发送时,

performSegueWithIdentifier 需要 String 格式。调用此函数的正确方法是

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

How to changed the button titled from selected Subject?

我猜您想在 savedSelectedSubject segue 之后呈现的 Controller 中反射(reflect)所选主题。假设 ViewController A 是您要推送/呈现的 Controller ,而 ViewController B 是要推送/呈现的 Controller 。请按照以下步骤操作:

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

    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. 在 Controller 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.
    

关于ios - 如何更改表格静态单元格的按钮标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42753291/

相关文章:

ios - 如何使用 Swift 以编程方式创建 UILabel?

ios - 如何在 iOS 中仅过滤 GMSAutocompleteResultsViewController 中的学校?

ios - AudioKit 4.1 - 如何正确停止 AKSampler 播放音频?

ios - 在 SwiftUI 中使用 UIViewControllerRepresentable 呈现 UIDocumentInteractionController

swift 3 - WKWebView 中的 http 身份验证

ios - 在运行时更改 UIButton 类

iOS 添加按钮到 UITableViewHeaderFooterView 的右侧

ios - 披露指示器移动单元格 subview

arrays - Swift 4 JSON Codable - 返回的值有时是一个对象,其他是一个数组

ios - 如何: Format Swift UI Buttons