我通过Swift中的默认Segue func将3个对象发送到DetailViewController(Title,Date,Body)。我只搜索标题数据。那部分工作得很好但是当点击SearchController中的单元格时,它会把我带到错误的视图。 (标题在视图中是正确的,但日期和正文不匹配)。
Segue func:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow {
var object = tableTitle[indexPath.row] as String
var objectTwo = detailDate[indexPath.row] as String
var objectThree = tableBody[indexPath.row] as String
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
if resultSearchController.isActive {
object = filteredTableData[indexPath.row]
} else {
object = tableTitle[indexPath.row]
objectTwo = detailDate[indexPath.row]
objectThree = tableBody[indexPath.row]
}
controller.detailItem = object
controller.detailItemTwo = objectTwo
controller.detailItemThree = objectThree
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
cellForRowAt func:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewCell
let imageView = UIImage(named: "dIndicator")
cell.accessoryView = UIImageView(image: imageView)
let object = tableTitle[indexPath.row]
let objectTwo = tableBody[indexPath.row]
let objectThree = tableDate[indexPath.row]
if self.resultSearchController.isActive {
cell.titleLabel!.text = filteredTableData[indexPath.row]
cell.detailLabel!.text = ""
cell.dateLabel!.text = ""
} else {
cell.titleLabel!.text = object
cell.detailLabel!.text = objectTwo
cell.dateLabel!.text = objectThree
}
return cell
}
updateSearchResults func:
func updateSearchResults(for searchController: UISearchController) {
filteredTableData.removeAll(keepingCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
let array = (tableTitle as NSArray).filtered(using: searchPredicate)
filteredTableData = array as! [String]
self.tableView.reloadData()
}
非常感谢任何帮助!
编辑:
struct objectsToPass {
var annTitle = [String]()
var annBody = [String]()
var annDate = [Date]()
var annDateALT = [Date]()
var tableData : [([String], [String], [Date], [Date])] {
get {
return [(annTitle, annBody, annDate, annDateALT)]
}
}
}
答案 0 :(得分:0)
试试这个:
struct myObject {
var title: String
var body: String
var date: Date
var altDate: Date
}
然后有两个数组用于表数据和过滤表数据
var tableData = [myObject]()
var filteredData = [myObject]()