ViewController用于过滤的TableViewCell

时间:2017-11-21 12:18:47

标签: ios xcode

我正在Xcode中创建一个搜索栏,并添加了tableViewCells。当我没有搜索并点击tableViewCells时,我将被引导到一个显示更多特定信息的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
}



}

有没有办法轻松地将ViewController添加到过滤后的tableViewCells?感谢所有帮助。

1 个答案:

答案 0 :(得分:0)

创建一个新的视图控制器并添加一个segue。如果需要,您可以查看documentationthis question以获取帮助。

然后你可以在didSelectRowAt

中调用你的segue
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)
        }
    }
}