[英]Pass information from a tableview to a view controller based not on indexPathForSelectedRow, but on label text of cell selected?
I have a tableview
inside of a UIViewController
, and on Main.storyboard
, I have a show
segue from the prototype cell on the tableview
in the first view controller to the second view controller . 我在UIViewController
里面有一个tableview
,在Main.storyboard
,我从第一个视图控制器中的tableview
上的原型单元到第二个视图控制器有一个show
segue。 I have this code: 我有这个代码:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showTheSeriesPage"
{
let indexPath : NSIndexPath = self.theTableAyy.indexPathForSelectedRow! as NSIndexPath
var destViewController = segue.destination as! SeriesPageViewController
var randomTemporaryArray = dataAry[indexPath.row]
destViewController.sampleTitle = randomTemporaryArray.objectSeriesTitle
destViewController.sampleAuthor = randomTemporaryArray.objectSeriesAuthor
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
It works, but only when the the tableview
is in a specific order and is based on the indexPath.row
, not on the specific details of the cell itself. 它可以工作,但只有当tableview
按特定顺序并且基于indexPath.row
,而不是基于单元本身的特定细节。 How do I set it so that it doesn't depend on the indexPath.row
selected, but maybe the label text of the cell? 如何设置它以使其不依赖于所选的indexPath.row
,但可能是单元格的标签文本? Thanks! 谢谢!
Short answer: Don't do that. 简短的回答:不要这样做。 Your current approach is the correct one. 您当前的方法是正确的。 You should have a data model that contains the information displayed in your table view. 您应该有一个包含表视图中显示的信息的数据模型。 When the user taps a cell, you should use the indexPath of the selected cell to look up the entry in your model, and then fetch whatever string is appropriate for that entry. 当用户点击一个单元格时,您应该使用所选单元格的indexPath来查找模型中的条目,然后获取适合该条目的任何字符串。
Cells are view objects and you should not use them to store information. 单元格是视图对象,您不应使用它们来存储信息。
I understand this problem as this is common when you link the TableView with the search button. 我理解这个问题,因为当您将TableView与搜索按钮链接时,这很常见。 Especially, if you have a search bar linked to table results to change dynamically. 特别是,如果您有一个链接到表格结果的搜索栏,则会动态更改。
I saw many posts using the prepareForSegue function but I wanted to use prepare function to segue to next view like you. 我看了很多使用prepareForSegue函数的帖子,但我想使用prepare函数来切换到你的下一个视图。 In my case, to have a more controlled code area that sends the segue in action from a single contained function. 就我而言,拥有一个更受控制的代码区域,可以从单个包含的函数中发送segue。
my solution: 1. I had to declare 2 variables in your view controller to store the filtered results (dynamic new list in table view) and the selected row. 我的解决方案:1。我必须在视图控制器中声明2个变量来存储过滤结果(表视图中的动态新列表)和所选行。 for example if we were dealing with car listings then, 例如,如果我们处理汽车清单,那么,
var filteredCars = [Car]()
var selectedCar: AnyObject!
then in the searchBar function: 然后在searchBar函数中:
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filteredCars = self.cars.filter({ (x: Car) -> Bool in
return x.name?.lowercased().range(of: searchText.lowercased()) != nil
})
self.tbvOriginalTable.reloadData()
// IMPORTANT store the new filtered table results to use them later
saveFilteredTable = self.tbvOriginalTable
}
now in the prepare function: 现在在准备功能:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "optionlist" {
let controller = segue.destination as! MealListTableViewController
// this is the declared @IBOutlet weak var searchCar: UISearchBar!
if searchCar.text != "" {
controller.car = filteredCars[(saveFilteredTable.indexPathForSelectedRow?.row)!]
} else {
controller.car = cars[(tbvOriginalTable.indexPathForSelectedRow?.row)!]
}
}
}
// We have to use the controller to assign an object of type car on the other side of the segue to receive the passed record
This really did the trick for me, it took me a bit of reading and research and finally got it done without needing to use func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) coupled with prepareForSegue function or any other type of long coding 这真的为我做了伎俩,它花了我一些阅读和研究,最后完成它而不需要使用func tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath)加上prepareForSegue函数或任何其他类型的长编码
I hope this is useful to many people as well. 我希望这对许多人也有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.