Swift:Tableview和Search Bar首次使用segue发送null对象

时间:2015-07-13 11:25:58

标签: ios xcode swift storyboard segue

我有一个带有搜索栏的tableview。搜索方法没问题,效果很好。我对 didSelectRowAtIndexPath 部分有疑问。

当我选择列表项(单元格)时,需要将此项目传递给具有segue的下一个场景。

但是当我选择一个单元格来显示项目细节时;

首选: null对象, 第二个选择:第一个对象, 第三个选择:第二个对象传递到下一个场景。

我的车型:

class Vehicle {
    var owner : String 
    var plate : String 
    var location : String 
    var isOnline : String 
    var state : String 
    var speed : String 
    var driver : String 
    var maxSpeed : String
    var dailyOverSpeed : String 
    var energy : String 
    var firstEngineDate : String 
    var messageDate : String 
    var dailyKm : String 
    var totalMoving : String 
    var totalWaiting : String 
    var totalParking : String 
    var totalKm : String 


    init() {
        self.owner = ""
        self.plate = ""
        self.location = ""
        self.isOnline = ""
        self.state = ""
        self.speed = ""
        self.driver = ""
        self.maxSpeed = ""
        self.dailyOverSpeed = ""
        self.energy = ""
        self.firstEngineDate = ""
        self.messageDate = ""
        self.dailyKm = ""
        self.totalMoving = ""
        self.totalWaiting = ""
        self.totalParking = ""
        self.totalKm = ""
    }

    init(owner: String, plate: String, location: String, isOnline: String, state: String, speed: String, driver: String, maxSpeed: String, dailyOverSpeed: String, energy: String, firstEngineDate: String, messageDate: String, dailyKm: String, totalMoving: String, totalWaiting: String, totalParking: String, totalKm: String) {
        self.owner = owner
        self.plate = plate
        self.location = location
        self.isOnline = isOnline
        self.state = state
        self.speed = speed
        self.driver = driver
        self.maxSpeed = maxSpeed
        self.dailyOverSpeed = dailyOverSpeed
        self.energy = energy
        self.firstEngineDate = firstEngineDate
        self.messageDate = messageDate
        self.dailyKm = dailyKm
        self.totalMoving = totalMoving
        self.totalWaiting = totalWaiting
        self.totalParking = totalParking
        self.totalKm = totalKm
    }
}

我的tableview控制器:

class VehiclesTableViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate{
...
    var vehicles = [Vehicle]()
    var filteredVehicles = [Vehicle]()
    var selectedVehicle = Vehicle()
.....
}

didSelectRowAtIndexPath

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        if (tableView == self.tableView) {
            self.selectedVehicle = vehicles[indexPath.row]
        } else {

            self.selectedVehicle = filteredVehicles[indexPath.row]
        }


        println(self.selectedVehicle.plate)

    }

prepareForSegue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "DisplayViewSegue" {
            var nextScene  =  segue.destinationViewController as! DisplayViewController
            nextScene.currentVehicle = self.selectedVehicle


        }
    }

当我选择任何单元格(搜索或不搜索)时,我的println()工作正确,这在didSelectRowAtIndexPath中。困惑?

1 个答案:

答案 0 :(得分:0)

听起来您可能将搜索栏作为tableview的顶部元素,因此当您从车辆阵列访问元素时,它需要是indexPath.row-1。此外,如果你只选择一行时你可以直接使用self.tableview.indexPathForSelectedRow()访问索引,并使用它来获得正确的车辆希望这可以解决你的问题