【问题标题】:How to pass data from one view controller to the other SWIFT如何将数据从一个视图控制器传递到另一个 SWIFT
【发布时间】:2014-12-27 00:58:33
【问题描述】:

我正在制作一个应用程序,其中带有搜索栏和范围栏的表格视图必须与详细视图控制器相匹配,并且该详细视图控制器必须根据所选单元格显示数据。我有一个数组,其中设置了用于排序和搜索项目的结构。我需要保留此功能,我的详细视图控制器有另一个 swift 类,我将把 if/else 语句放入该处理中,以根据哪个单元格显示数据 我选择。我需要知道如何将变量附加到单元格并将该变量传递给详细视图控制器以在我的 if/else 语句中使用,以便我可以显示数据。如果这不是正确的方法,请告诉我。

结构代码

struct Booth {
    let category : String
    let name : String
}

表格视图控制器代码

import UIKit

class BoothsTableViewController: UITableViewController {    

var booths = [Booth]()
var filteredBooths = [Booth]()

override func viewDidLoad() {
    super.viewDidLoad()

    //fill array with data
    self.booths = [Booth(category: "Tech", name: "Conference App"),
        Booth(category: "Tech", name: "Space Shooter"),
        Booth(category: "Tech", name: "RollABall"),
        Booth(category: "Animation", name: "Sugar Hill City Model"),
        Booth(category: "Animation", name: "3D Sculpting 101"),
        Booth(category: "Animation", name: "HowTo - Texture"),
        Booth(category: "Science", name: "AP Biology for Dummies"),
        Booth(category: "Science", name: "Cells"),
        Booth(category: "Science", name: "Space")]

    //reload the table
    self.tableView.reloadData()
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView == self.searchDisplayController!.searchResultsTableView {
        return self.filteredBooths.count
    } else {
        return self.booths.count
    }
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    //ask for a reusable cell from the tableview, the tableview will create a new one if it doesn't have any
    let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell

    var boothsCheck : Booth
    // Check to see whether the normal table or search results table is being displayed and set the Booth object from the appropriate array
    if tableView == self.searchDisplayController!.searchResultsTableView {
        boothsCheck = filteredBooths[indexPath.row]
    } else {
        boothsCheck = booths[indexPath.row]
    }

    // Configure the cell
    cell.textLabel.text = boothsCheck.name
    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

    return cell
}

func filterContentForSearchText(searchText: String, scope: String = "All") {
    self.filteredBooths = self.booths.filter({( Booth : Booth) -> Bool in
        var categoryMatch = (scope == "All") || (Booth.category == scope)
        var stringMatch = Booth.name.rangeOfString(searchText)
        return categoryMatch && (stringMatch != nil)
    })
}

func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {
    let scopes = self.searchDisplayController!.searchBar.scopeButtonTitles as [String]
    let selectedScope = scopes[self.searchDisplayController!.searchBar.selectedScopeButtonIndex] as String
    self.filterContentForSearchText(searchString, scope: selectedScope)
    return true
}

func searchDisplayController(controller: UISearchDisplayController!,
    shouldReloadTableForSearchScope searchOption: Int) -> Bool {
        let scope = self.searchDisplayController!.searchBar.scopeButtonTitles as [String]
        self.filterContentForSearchText(self.searchDisplayController!.searchBar.text, scope: scope[searchOption])
        return true
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.performSegueWithIdentifier("BoothDetail", sender: tableView)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "BoothDetail" {

        let BoothDetailViewController = segue.destinationViewController as UIViewController


        if sender as UITableView == self.searchDisplayController!.searchResultsTableView {
            let indexPath = self.searchDisplayController!.searchResultsTableView.indexPathForSelectedRow()!
            let destinationTitle = self.filteredBooths[indexPath.row].name
            BoothDetailViewController.title = destinationTitle


    } else {

            let indexPath = self.tableView.indexPathForSelectedRow()!
            let destinationTitle = self.booths[indexPath.row].name
            BoothDetailViewController.title = destinationTitle
            }
        }
    }
}

转义代码

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "BoothDetail" {

        let BoothDetailViewController = segue.destinationViewController as UIViewController


        if sender as UITableView == self.searchDisplayController!.searchResultsTableView {
                let indexPath = self.searchDisplayController!.searchResultsTableView.indexPathForSelectedRow()!
                let destinationTitle = self.filteredBooths[indexPath.row].name
                BoothDetailViewController.title = destinationTitle        
        } else {            
                let indexPath = self.tableView.indexPathForSelectedRow()!
                let destinationTitle = self.booths[indexPath.row].name
                BoothDetailViewController.title = destinationTitle
            }
        }
    }
}

【问题讨论】:

    标签: ios swift view segue


    【解决方案1】:

    在detailViewController 中创建一个成员变量/对象。在当前 TableViewController 中的 prepareforSegue 中设置此对象。这样,detailViewController 将拥有用户单击的单元格对象。

    【讨论】:

      【解决方案2】:

      例如,在您的 BoothsTableViewController 创建变量来保存当前选定的 Booth,您在 didSelectRowAtIndexPath 中分配。然后,您可以在 prepareForSegue 方法中将此变量传递给详细视图控制器。

      【讨论】:

      • 是的,谢谢,问题是我不知道如何通过它@Rick
      • 也只需在详细视图中创建一个变量,您可以在创建 DetailViewController 后设置它,就像您对标题所做的那样。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多