gpt4 book ai didi

iphone - 将搜索添加到 TableViewController 并显示结果

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

所以我的问题是我想在 FirstSearch.swift 列表中搜索名称,但我也想在搜索结果中显示图像和城镇。我不知道如何做到这一点,而且我对 Swift 编程有点陌生。那么有人可以帮我解决我的代码

我的第一次搜索.swift

import Foundation

class People {
class Input {
let imageName: String
let userName: String
let userTown: String
init(iName: String, uName: String, uTown: String){
self.imageName = iName
self.userName = uName
self.userTown = uTown
}
}

let peopleList = [
Input(iName: "Habib.jpg", uName: "Habib Badiane", uTown: "Emden, Deutschland"),
Input(iName: "John.jpg", uName: "John Adams", uTown: "Emden, Deutschland"),
Input(iName: "Marcus.jpg", uName: "Marcus Dirks", uTown: "Emden, Deutschland"),
Input(iName: "0.jpg", uName: "Tobias Tebben", uTown: "Emden, Deutschland"),
Input(iName: "Neda.jpg", uName: "Neda Heidari", uTown: "Emden, Deutschland"),
Input(iName: "Gudrun.jpg", uName: "Gudrun Burlefinger", uTown: "Emden, Deutschland")
]

}

我的 FirstSearchTableViewController.swift

import UIKit

class FirstSearchTableViewController: UITableViewController, UISearchResultsUpdating {

let people = People()
var filteredTableData = [String]()
var resultSearchController = UISearchController()

override func viewDidLoad() {
super.viewDidLoad()

self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()

self.tableView.tableHeaderView = controller.searchBar

return controller
})()

// Reload the table
self.tableView.reloadData()

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (self.resultSearchController.active) {
return self.filteredTableData.count
}
else {
return people.peopleList.count
}
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! FirstSearchTableViewCell

let input = people.peopleList[indexPath.row]
let image = UIImage(named: input.imageName)
cell.profileImg.image = image
cell.profileName.text = input.userName
cell.profileCity.text = input.userTown



// Configure the cell...

if (self.resultSearchController.active) {
cell.textLabel?.text = filteredTableData[indexPath.row]

return cell
}
else {
cell.textLabel?.text = people.peopleList[indexPath.row]

return cell
}
}

func updateSearchResultsForSearchController(searchController: UISearchController)
{
filteredTableData.removeAll(keepCapacity: false)

let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
let array = (people.peopleList as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredTableData = array as! [String]

self.tableView.reloadData()
}
}

最佳答案

您的主要问题是您的表函数必须标识您希望显示的列。寻找一些简单的示例代码。

每次调用 tableView 都必须检查列和行。您的代码只能返回名称,因为它不检查该列。这些列由您在 IB(界面构建器)中指定的名称来标识。这是一个例子

     if (tableColumn!.identifier == "name")
{
return artistSet!.itemAt(row,key:"name")
}
else if (tableColumn!.identifier == "image")
{
// etc
}

关于iphone - 将搜索添加到 TableViewController 并显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33743910/

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