如何从表格静态单元格更改按钮标题? [英] How to change button title from table static cells?

查看:53
本文介绍了如何从表格静态单元格更改按钮标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有HomeViewController,它以模态形式绑定到导航控制器,其标识符为: pickSubjectAction

I have HomeViewController that's segued modally to a Navigation Controller with an identifier of: pickSubjectAction

然后在SubjectPickerTableViewController上选择我的主题.这是我的代码

And on SubjectPickerTableViewController is where my subjects to choose. This is my code

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控制器区分开?我尝试了这个但是失败了Q2:如何更改所选主题的标题按钮?

Q1: How can i segued from button to the tableview controller? I tried this but failed Q2: How to changed the button titled from selected Subject?

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

感谢您的帮助.谢谢

推荐答案

如何从按钮到Tableview控制器隔离?我试过了这但是失败了

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

performSegueWithIdentifier 在以 Any 格式发送时,要求使用 String 格式.调用此函数的正确方法是

performSegueWithIdentifier expects a String format while you are sending it in Any format. Correct way for call this function is

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

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

How to changed the button titled from selected Subject?

我想您想在 savedSelectedSubject 设置后显示的控制器中反映所选主题.假设ViewController A是您要推送/呈现的对象,而ViewController B是您要推送/呈现的对象.请按照以下步骤操作:

I guess you want to reflect the selected subject in the controller presented after savedSelectedSubject segue. Let ViewController A be the one through which you are pushing/presenting and ViewController B be the one which is pushed/presented. Follow the following steps :

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

  1. For this you need to fetch the destination controller (B) from prepare(for segue: UIStoryboardSegue, sender: Any?) function via segue.destination property.
  2. Make a public variable in B in which you can set your selected subject. Using that property you can show your selected subject title.

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

  • 在控制器B中

  • In 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.
    

  • 这篇关于如何从表格静态单元格更改按钮标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆