简体   繁体   English

将信息从tableview传递给视图控制器而不是基于indexPathForSelectedRow,而是传递给选定单元格的标签文本?

[英]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.

相关问题 如何将单元格标签的值从tableView传递到其他View? - How to pass cell label's value from tableView to other View? 仅当选择按钮(复选框)时,才使用segues将标签文本从一个视图控制器传递到另一个视图控制器 - Pass label text from one view controller to another using segues only if button (checkbox) is selected 如何根据表视图单元格的详细文本标签,将segue设置为与表视图控制器不同的视图控制器? - How can I set a segue to a different view controller from a table view controller, based on the detail text label of the table view cell? 将文本从单元格移动到另一个视图控制器中的文本标签 - Move Text from cell to text label in a different view controller 如何使用CoreData从选定的集合视图单元格中获取标签文本? - How to Get Label Text from Selected Collection View Cell with CoreData? 如何根据位于同一视图 Controller 中的选定 tableView 单元格更新标签? - How do I update labels based on a selected tableView cell that is in the same View Controller? 将核心数据从选定的表格单元格传递到新的视图控制器 - Pass core data from selected table cell to new view controller 如何将选定的tableview单元格文本发送到iOS中的前一个视图控制器,Objective C(向后传递数据) - How to send selected tableview cell text to previous view controller in iOS, Objective C (passing data backward) 如何将信息从一个视图控制器中的表视图单元传递到另一视图控制器? - How do I pass information from a table view cell in one view controller to another view controller? 使用来自单独的表视图控制器的选定单元格填充文本字段 - Populating a Text Field with a Selected Cell from a Separate Table View Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM