gpt4 book ai didi

ios - Swift:Tableview 和搜索栏第一次使用 segue 发送 null 对象

转载 作者:行者123 更新时间:2023-11-30 14:14:15 25 4
gpt4 key购买 nike

我有一个带有搜索栏的表格 View 。搜索方法还可以,效果很好。我的 didSelectRowAtIndexPath 部分有问题。

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

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

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

我的车辆类别:

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
}
}

我的表格 View Controller :

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)

}

准备Segue:

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


}
}

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

最佳答案

听起来您可能将搜索栏作为表格 View 的顶部元素,因此当您访问车辆数组中的元素时,它需要是indexPath.row-1。另外,如果您仅在选择一行时进行segue,则可以使用 self.tableview.indexPathForSelectedRow() 直接访问索引并使用它来获取正确的车辆,希望这可以解决您的问题

关于ios - Swift:Tableview 和搜索栏第一次使用 segue 发送 null 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31382120/

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