검색 막대와 범위 막대가있는 테이블보기가 세부보기 컨트롤러와 연결되어야하는 세부적인보기 컨트롤러가 기반으로 데이터를 표시해야하는 응용 프로그램을 만들고 있습니다. 어느 셀이 선택되는지. 정렬 및 항목을 검색하도록 구조체가 설정된 배열이 있습니다. 이 기능을 유지할 필요가 있습니다. 세부 뷰 컨트롤러를위한 또 다른 신속한 클래스가 있습니다. if/else 문을 셀에 기반하여 데이터를 표시하는 거래에 넣을 것입니다. 을 선택합니다. 변수를 셀에 연결하고 해당 변수를 상세 뷰 컨트롤러에 전달하여 if/else 문에서 사용하여 데이터를 표시 할 수 있어야합니다. 이것이 올바른 방법이 아니라면 알려주십시오.하나의보기 컨트롤러에서 다른 SWIFT로 데이터를 전달하는 방법
구조체 코드
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
}
}
}
}
네, 고맙습니다. 그 일은 정확히 어떻게 해야할지 모르겠습니다. @Rick – user3558131
마찬가지로 DetailView에서 변수를 생성하기 만하면됩니다. 마찬가지로 DetailViewController를 만든 후에 설정할 수 있습니다. 제목으로해라. – Rick