如何从字符串数组中获取文本并将其用作按钮的文本?

时间:2017-08-23 22:50:00

标签: arrays swift string uibutton

我正在尝试使用包含此信息的数组:[“Concert”,“Coachella”,“Date”]并将所选数组的文本转换为按钮文本。因此,当我在数组中选择“date”选项(在表格视图中显示)时,应该将该数据发送回我的第一个VC,其中文本“date”显示为按钮上的文本。出于某种原因,我似乎无法从[String]中提取String。一旦我提取了该字符串,我就可以将该字符串设置为我的UIButton的标题文本。

以下是表视图显示选项的位置的代码,用户选择一个选项并将数据发送到另一个VC:

var delegate : SentDataBack?

@IBOutlet weak var occasionsTableView: UITableView!
@IBOutlet weak var occasionSearchBar: UISearchBar!

var addedOccasions = ["Coachella", "Concert", "Date"]

var index = 0

var selectedCell = [""]

override func viewDidLoad() {
    super.viewDidLoad()

    occasionsTableView.delegate = self
    occasionsTableView.dataSource = self

    // Do any additional setup after loading the view.
}

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

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

    let cell = occasionsTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    //let addedOccasions = ["Coachella", "Concert", "Date"]

    cell.textLabel?.text = addedOccasions[indexPath.row]

    cell.textLabel?.highlightedTextColor = UIColor(red: 255/255, green: 77/255, blue: 91/255, alpha: 1)

    return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return(addedOccasions.count)

}

// need to create a method that determines what happens when you click on a cell. what it should do is highlight its text then either store that name into a variable or return what cell text was selected to the data sent back method 
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    index = indexPath.row

    selectedCell = [addedOccasions[index]]

}

@IBAction func doneButtonPressed(_ sender: Any) {

    delegate?.dataSentBack(occasionSentBack: selectedCell)
    // sends back selected option
    dismiss(animated: true, completion: nil) //can also add a function in here where it says nil, if you want something to happen upon completion of dismiss
}

这是接收该数据的代码,并且应该操作一个按钮来显示所选[String]的文本:

func dataSentBack(occasionSentBack: [String]) {

configureAddedOccasionButton(selectedOccasion: occasionSentBack)

    // once you get back whatever data is sent back which should be one event, then this area should add that button as selected to the filter list and possibly should add occasion name to occasion array list
}

func configureAddedOccasionButton(selectedOccasion : [String]) {

    //set look of occasion button
    addedOccasion.isHidden = false
    addedOccasion.titleLabel?.text = selectedOccasion.description
    addedOccasion.frame = CGRect(x: 86, y: 33, width: 70, height: 43)
    buttonSelected(buttonPressed: addedOccasion)

}

1 个答案:

答案 0 :(得分:0)

setButton标题中的问题如果selectedOccasion是一个字符串数组,你需要通过索引传递给你的委托来从数组中获取文本

 var delegate : SentDataBack?

    @IBOutlet weak var occasionsTableView: UITableView!
    @IBOutlet weak var occasionSearchBar: UISearchBar!

    var addedOccasions = ["Coachella", "Concert", "Date"]

    var index = 0

    var selectedCell = ""


    override func viewDidLoad() {
        super.viewDidLoad()

        occasionsTableView.delegate = self
        occasionsTableView.dataSource = self

        // Do any additional setup after loading the view.
    }

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

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

        let cell = occasionsTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        //let addedOccasions = ["Coachella", "Concert", "Date"]

        cell.textLabel?.text = addedOccasions[indexPath.row]

        cell.textLabel?.highlightedTextColor = UIColor(red: 255/255, green: 77/255, blue: 91/255, alpha: 1)

        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return(addedOccasions.count)

    }

    // need to create a method that determines what happens when you click on a cell. what it should do is highlight its text then either store that name into a variable or return what cell text was selected to the data sent back method
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        index = indexPath.row

        selectedCell = addedOccasions[index]

    }

    @IBAction func doneButtonPressed(_ sender: Any) {

        delegate?.dataSentBack(occasionSentBack: selectedCell)
        // sends back selected option
        dismiss(animated: true, completion: nil) //can also add a function in here where it says nil, if you want something to happen upon completion of dismiss
    }

//////////

func dataSentBack(occasionSentBack: String) {

    configureAddedOccasionButton(selectedOccasion: occasionSentBack)

    // once you get back whatever data is sent back which should be one event, then this area should add that button as selected to the filter list and possibly should add occasion name to occasion array list
}

func configureAddedOccasionButton(selectedOccasion : String) {

    //set look of occasion button
    addedOccasion.isHidden = false
    addedOccasion.setTitle(selectedOccasion,for: .normal)

    addedOccasion.frame = CGRect(x: 86, y: 33, width: 70, height: 43)
    buttonSelected(buttonPressed: addedOccasion)

}