gpt4 book ai didi

ios - 在尝试从 tableView 中分离出来时发现 nil

转载 作者:行者123 更新时间:2023-11-28 08:38:28 25 4
gpt4 key购买 nike

每当我尝试在 viewDidLoad 中引用 viewTable 在我单击一个单元格以使用 segue 进行转换时,我都会收到错误消息。非常感谢!!

基本上我不能使用 segue,除非我在 View 中注释掉 tableview 引用确实加载了......但我需要那些才能使用搜索栏并且我确信它会在返回的路上引起问题......

    class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {


@IBOutlet var tableView: UITableView! {
didSet {
print("tableView is set")
}
}
let searchController = UISearchController(searchResultsController: nil)
let textCellIdentifier = "TextCell"
var buildings: [(String,String)] = []
var filteredBuildings = [(String,String)]()
var goToIndex: Int?

override func viewDidLoad() {



super.viewDidLoad()

print(tableView)
var buildingTuples = loadBuildings()

for tuple in buildingTuples {
self.buildings.append(tuple)
}


self.goToIndex = -1

searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
tableView!.tableHeaderView = searchController.searchBar

}


func filterContentForSearchText(searchText: String, scope: String = "All") {
filteredBuildings = buildings.filter { building in
return building.0.lowercaseString.containsString(searchText.lowercaseString)
}

self.tableView.reloadData()
}

// MARK: UITextFieldDelegate Methods
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//return self.buildings.count
if searchController.active && searchController.searchBar.text != "" {
return filteredBuildings.count
}
return buildings.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
/*
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath)

let row = indexPath.row
cell.textLabel?.text = buildings[row].0

return cell
*/
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath)
let tuple: (String, String)
if searchController.active && searchController.searchBar.text != "" {
tuple = filteredBuildings[indexPath.row]
} else {
tuple = buildings[indexPath.row]
}
cell.textLabel?.text = tuple.0

return cell
}

// MARK: UITableViewDelegate Methods
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//tableView.deselectRowAtIndexPath(indexPath, animated: true)

let row = indexPath.row
self.goToIndex = indexPath.row

self.performSegueWithIdentifier("MainToLocation", sender: self)
//print(buildings[row].0)
}

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

if segue.identifier == "MainToLocation" {
let locationViewController = (segue.destinationViewController as! LocationViewController)
locationViewController.building = self.buildings[self.goToIndex!]
}

}







extension ViewController: UISearchResultsUpdating {
func updateSearchResultsForSearchController(searchController: UISearchController) {
filterContentForSearchText(searchController.searchBar.text!)
}
}

最佳答案

你可以试试这个..

let destinationVC = self.storyboard!.instantiateViewControllerWithIdentifier("viewController") as! NextViewController


var alreadyPushed = false

if let vc = self.navigationController?.viewControllers {
for viewController in vc {
if let viewController = viewController as? NextViewController {
self.navigationController?.popToViewController(viewController, animated: true)
print("Push your controller")
alreadyPushed = true
break
}
}
}
if alreadyPushed == false {
self.navigationController?.pushViewController(destinationVC, animated: true)
}

关于ios - 在尝试从 tableView 中分离出来时发现 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37015529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com