如何在没有搜索栏的情况下在表视图中按对象设置过滤器?
How to set filter by object in tableview without search bar?
我有一个 table 视图,我想添加一个按给定选项过滤数据的按钮。例如,我只想显示 table 中包含“参加”的数据。
这是我的代码:
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: { [=10=].attendingStatus ?? "" > .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
}
型号:
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?
}
为了实现您想要的效果,您可以使用三个全局变量
- 您在开头初始化的完整列表(guestsList)
- 过滤列表,用作table查看数据源(filteredGuestsList)
- 搜索到的字符串,它将过滤器应用于列表(在我的例子中,考虑到您将过滤器应用于
guestName
属性)
filterBy
方法只需要更新搜索值,每次清除或更新搜索值时都需要调用
private var guestsList = [GuestModel]() {
didSet {
filteredGuestsList = guestsList
}
}
private var filteredGuestsList = [GuestModel]() {
didSet {
guestsTableView.reloadData()
}
}
private var search = "" {
didSet {
filteredGuestsList = search != "" ? guestsList.filter {
[=10=].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
}
我有一个 table 视图,我想添加一个按给定选项过滤数据的按钮。例如,我只想显示 table 中包含“参加”的数据。 这是我的代码:
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: { [=10=].attendingStatus ?? "" > .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
}
型号:
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?
}
为了实现您想要的效果,您可以使用三个全局变量
- 您在开头初始化的完整列表(guestsList)
- 过滤列表,用作table查看数据源(filteredGuestsList)
- 搜索到的字符串,它将过滤器应用于列表(在我的例子中,考虑到您将过滤器应用于
guestName
属性)
filterBy
方法只需要更新搜索值,每次清除或更新搜索值时都需要调用
private var guestsList = [GuestModel]() {
didSet {
filteredGuestsList = guestsList
}
}
private var filteredGuestsList = [GuestModel]() {
didSet {
guestsTableView.reloadData()
}
}
private var search = "" {
didSet {
filteredGuestsList = search != "" ? guestsList.filter {
[=10=].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
}