2017-11-21 2 views
0

Xcode에서 검색 막대를 만들고 tableViewCells를 추가했습니다. tableViewCell을 검색하고 클릭하지 않을 때는보다 구체적인 정보를 표시하는 viewController로 안내 될 것입니다. 그러나 실제로 검색 막대에서 무언가를 검색하고 tableViewCell을 클릭하면 viewController가 없습니다. 나는 어디가 잘못되었는지 확신하지 못한다. 나는 아마추어 다.필터링 된 TableViewCell에 대한 ViewController

import UIKit 

var roomies = [Room]() 
var text = ["1st Floor of the Upper Building","2nd Floor of the Upper 
Building","3rd Floor of the Upper Building","4th Floor of the Upper 
Building","5th Floor of the Upper Building","6th Floor of the Upper 
Building","1st Floor of the Intermediate Building","2nd Floor of the 
Intermediate Building","1st Floor of the Lower Building","2nd Floor of the 
Lower Building","3rd Floor of the Lower Building","4th Floor of the Lower 
Building","1st Floor of the Rittmann","2nd Floor of the Rittmann","3rd Floor 
of the Rittmann","4th Floor of the Rittmann","1st Floor of the Phoenix 
Center","2nd Floor of the Phoenix Center"] 
var row = 0 

class LocationSearch: UITableViewController, UISearchResultsUpdating{ 




var filtered = [Room]() 
var searchController : UISearchController! 
var resultsController = UITableViewController() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    roomies = [ 
     Room(roomName:"H316/H318, Wittenberg Hall", floorNumber: 2), 
     Room(roomName:"PC Lounge", floorNumber:2), 
     Room(roomName:"H115, HS Gym, ECOSOC", floorNumber: 0), 
     Room(roomName:"P228, Phoenix Café", floorNumber: 1), 
     Room(roomName:"H227, HS Library, Approval Panel", floorNumber: 1), 
     Room(roomName: "H233, HS Conference Room", floorNumber: 1), 
     Room(roomName:"H405/H406, Augsburg Hall, Environmental Committee", floorNumber: 3), 
     Room(roomName:"P106, PC Gym", floorNumber: 16), 
     Room(roomName:"M116, MS Gym", floorNumber: 7), 
     Room(roomName:"E322, ES Library, HRC", floorNumber: 10), 
     Room(roomName:"E122, ES Cafeteria", floorNumber: 8), 
     Room(roomName:"R302, Orchestra Room", floorNumber: 14), 
     Room(roomName:"PC Commons", floorNumber: 16), 
     Room(roomName:"H116, Old Luther, ICJ", floorNumber: 0), 
     Room(roomName:"P230/P231, Jade/Pearl, CSW", floorNumber: 17), 
     Room(roomName:"R110, Costume Room, Secretariat/Hive", floorNumber: 12), 
     Room(roomName:"H127, Health Room", floorNumber: 0), 
     Room(roomName:"R101, Rittmann Theatre", floorNumber: 12), 
     Room(roomName:"H305, CISSMUN Vigil", floorNumber: 2), 
     Room(roomName:"H404, Spec Conf", floorNumber: 3), 
     Room(roomName:"H403, UNPFII", floorNumber:3), 
     Room(roomName:"P201, New Luther, ICC", floorNumber: 17), 
     Room(roomName:"H205, Printing", floorNumber: 1), 
     Room(roomName:"P106, Phoenix Center Gym, Keynote Speakers", floorNumber:16), 
     Room(roomName:"Classroom next to Jade/Pearl???", floorNumber: 17), 
     Room(roomName:"H203", floorNumber:1), 
     Room(roomName:"H204", floorNumber:1), 
     Room(roomName:"H206", floorNumber:1), 
     Room(roomName:"MS and HS classrooms Schools???", floorNumber:0), 
     Room(roomName:"MSR???",floorNumber:6) 
    ] 


    searchController = UISearchController(searchResultsController: resultsController) 
    tableView.tableHeaderView = searchController.searchBar 
    searchController.searchResultsUpdater = self 
    resultsController.tableView.dataSource = self 
    resultsController.tableView.delegate = self 
    searchController.dimsBackgroundDuringPresentation = false 
    definesPresentationContext = true 
    searchController.hidesNavigationBarDuringPresentation = false 
    //searchController.searchBar.barTintColor = UIColor.black 
} 



func updateSearchResults(for searchController: UISearchController) { 
    filtered = roomies.filter({(room:Room) -> Bool in 
     return room.roomName.lowercased().contains(self.searchController.searchBar.text!.lowercased()) 

    }) 
    resultsController.tableView.reloadData() 
} 



// MARK: - Table view data source 
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    if tableView == resultsController.tableView{ 
     return filtered.count 
    } 
    else { 
     return roomies.count 
    } 

} 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    if tableView == resultsController.tableView{ 
     let cell = UITableViewCell() 
     cell.textLabel?.text = filtered[indexPath.row].roomName 
     return cell 
    } 
    else { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell 
     cell.textLabel?.text = roomies[indexPath.row].roomName 
     return cell 
    } 

} 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    row = indexPath.row 
} 
/* 
func tableView(_ tableView: UITableView, didSelectRow indexPath: IndexPath) { 

} 
*/ 
} 

위의 코드는 tableViewCells입니다. 다음은 tableViewCell에 연결하는 ViewController 코드입니다. 수입 UIKit

클래스 CellsViewController : UIViewController에, UIScrollViewDelegate {

@IBOutlet weak var imageView: UIImageView! 
@IBOutlet weak var scroll: UIScrollView! 
@IBOutlet weak var textView: UILabel! 
@IBOutlet weak var textViewtwo: UILabel! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    imageView.image = images[(NSInteger)(roomies[row].floorNumber)] 
    scroll.delegate = self 
    textView.text = roomies[row].roomName 
    textViewtwo.text = "Located on the " + text[ (NSInteger)(roomies[row].floorNumber) ] 
    //[@"7" intValue]; 
    // Do any additional setup after loading the view. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

func viewForZooming(in scrollView: UIScrollView) -> UIView? { 
    return imageView 
} 



} 

쉽게 필터링 tableViewCells에의 ViewController를 추가 할 수있는 방법이 있나요? 모든 도움을 주시면 감사하겠습니다.

+0

어떻게 현재 segueing 있습니다에 SEGUE 호출 할 수 있습니다? didSelectRowAt가 그렇게하지 않는 것 같습니다. 그래서 그 코드는 어디에 있습니까? – DoesData

+0

Main.storyboard를 통해 segueing 해요. segue가 올바르게 작동하도록 코드를 추가해야합니까? – haost

답변

0

새로운보기 컨트롤러를 만들고 segue를 안으로 추가하십시오. 필요한 경우 documentation 또는 this question을 볼 수 있습니다. 당신이 셀을 선택할 때

그럼 당신은 didSelectRowAt

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    if tableView == resultsController.tableView{ 
     DispatchQueue.main.async { 
      self.performSegue(withIdentifier: "toWhereYouWantWhenFilterIsActive", sender: nil) 
     } 
    } 
    else { 
     DispatchQueue.main.async { 
      self.performSegue(withIdentifier: "toWhereYouWantWhenNotFiltered", sender: nil) 
     } 
    } 
} 
관련 문제