![](/img/trans.png)
[英]How to implement TextField search bar to filter tableview values Swift
[英]How to set filter by object in tableview without search bar?
我有一個表格視圖,我想添加一個按鈕,該按鈕將按給定選項過濾數據。 例如,我只想顯示表中包含“參加”的數據。 這是我的代碼:
func filterBy(){
let addEvent = UIAlertController(title: "Sort by:", message: "Sort guests", preferredStyle: .alert)
let subview = (addEvent.view.subviews.first?.subviews.first?.subviews.first!)! as UIView
let shapes = UIView()
shapes.layer.cornerRadius = 30
shapes.layer.borderWidth = 5
shapes.layer.borderColor = #colorLiteral(red: 0.9843137255, green: 0.8196078431, blue: 0.8509803922, alpha: 1)
subview.backgroundColor = #colorLiteral(red: 0.9607843137, green: 0.9333333333, blue: 0.9176470588, alpha: 1)
subview.tintColor = #colorLiteral(red: 0.9843137255, green: 0.8196078431, blue: 0.8509803922, alpha: 1)
addEvent.view.tintColor = #colorLiteral(red: 0.4431372549, green: 0.4431372549, blue: 0.4431372549, alpha: 1)
let addSortAction = UIAlertAction(title: "Attending", style: .default) { (action) in
let sortedBy: () = self.guestsList.sort(by: { $0.attendingStatus ?? "" > $1.attendingStatus ?? ""})
print(sortedBy)
self.guestsTableView.reloadData()
}
let cancelAction = UIAlertAction(title: "Cancel", style: .default)
addEvent.addAction(addSortAction)
addEvent.addAction(cancelAction)
present(addEvent, animated: true, completion: nil)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return guestsList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "guestCell", for: indexPath) as! GuestTableViewCell
let guest: GuestModel
guest = guestsList[indexPath.row]
cell.guestNameLabel.text = guest.guestName! + " " + guest.guestFamilyName!
cell.attendingLabel.text = guest.attendingStatus
cell.menuLabel.text = guest.menuStatus
cell.ageLabel.text = guest.ageStatus
cell.tableLabel.text = guest.tableNo
return cell
}
model:
class GuestModel {
var guestName: String?
var attendingStatus: String?
var menuStatus: String?
var ageStatus: String?
var tableNo: String?
var id: String?
var guestFamilyName: String?
var guestPhoneNumber: String?
var guestEmail: String?
}
為了實現你想要的,你可以使用三個全局變量
guestName
屬性) filterBy
方法只需要更新搜索到的值,每次要清除或更新搜索到的值時都需要調用該方法。
private var guestsList = [GuestModel]() {
didSet {
filteredGuestsList = guestsList
}
}
private var filteredGuestsList = [GuestModel]() {
didSet {
guestsTableView.reloadData()
}
}
private var search = "" {
didSet {
filteredGuestsList = search != "" ? guestsList.filter {
$0.guestName.uppercased().contains(searchText.uppercased())
} : guestsList
}
}
func filterBy(_ text: String) {
search = text
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredGuestsList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "guestCell", for: indexPath) as! GuestTableViewCell
let guest = filteredGuestsList[indexPath.row]
cell.guestNameLabel.text = guest.guestName! + " " + guest.guestFamilyName!
cell.attendingLabel.text = guest.attendingStatus
cell.menuLabel.text = guest.menuStatus
cell.ageLabel.text = guest.ageStatus
cell.tableLabel.text = guest.tableNo
return cell
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.