我正在尝试获取一个包含以下信息的数组:[“音乐会”、“科切拉”、“日期”] 并将所选数组的文本转换为按钮的文本。因此,当我在数组中选择“日期”选项(在 TableView 中显示)时,应该将该数据发送回我的第一个 VC,其中文本“日期”显示为按钮上的文本。由于某种原因,我似乎无法从 [String] 中提取字符串。提取该字符串后,我可以将该字符串设置为 UIButton 的标题文本。
下面是表格 View 显示选项、用户选择一个选项并将数据发送到另一个 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)
}
最佳答案
setButton 标题中的问题也是,如果 selectedOccasion 是一个字符串数组,您需要通过索引从数组中获取文本传递给您的委托(delegate)
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)
}
关于arrays - 如何从字符串数组中获取文本并将其用作按钮的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45850446/