如何将数据从一个视图控制器传递到另一个SWIFT

时间:2014-10-30 20:04:29

标签: ios view swift segue detail

我正在创建一个应用程序,其中带有搜索栏和范围栏的表格视图必须切换到详细视图控制器,并且详细视图控制器必须根据选择的单元格显示数据。我有一个数组,其结构设置为排序和搜索项目。我需要保留这个功能,我有另一个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
            }
        }
    }
}

Segue code

    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
            }
        }
    }
}

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

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

相关问题