我有一个NSMutableArray
数组,如下所示
var myArr = NSMutableArray() {
studentId = 1437404654;
name = "Rancho";
},
{
studentId = 1932617669;
name = "Mastora";
},
{
studentId = 1457367179;
name = "Amanda";
}
我再拿一个NSMutableArray进行排序
var copyArr = NSMutableArray()
@IBOutlet weak var resultTable: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
var searchActive : Bool = false
我使用myArr
作为行数
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myArr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyCell
let dict = self. myArr[indexPath.row] as! NSDictionary
cell.idTextField.text = (dict.value(forKey: "studentId") as! String)
cell.nameTextField.text = (dict.value(forKey: "name") as! String)
return cell
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchActive = true;
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
searchActive = false;
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchActive = false;
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar){
searchActive = false;
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
let predicate = NSPredicate(format: "%K CONTAINS[cd] %@ OR %K CONTAINS[cd] %@", "studentId", searchText, "name", searchText)
let sorted = copyArr.filter({
return predicate.evaluate(with: $0)
})
print(sorted)
myArr.removeAllObjects()
if sorted.count==0
{
myArr.addObjects(from: copyArr as! [Any])
}
else {
myArr.addObjects(from: sorted)
}
resultTable.reloadData()
}
但是表未排序。请告诉我解决方法
答案 0 :(得分:1)
据我了解,您需要对数组进行排序。表格视图就是UI。因此,在为表视图分配值之前,需要对任何数组进行排序。
<PropertyGroup>
您可以使用以下方式对蚂蚁阵列进行排序
Proposed Steps:
1. Get data Assign In Array (Whatever data source)
2. Sort An Array
3. Reload Table View
OR
struct Contact {
var firstName: String
var lastName: String
}
var contacts = [
Contact(firstName: "Leonard", lastName: "Charleson"),
Contact(firstName: "Michael", lastName: "Webb"),
Contact(firstName: "Charles", lastName: "Alexson"),
Contact(firstName: "Michael", lastName: "Elexson"),
Contact(firstName: "Alex", lastName: "Elexson"),
]
contacts.sort {
($0.lastName, $0.firstName) <
($1.lastName, $1.firstName)
}
print(contacts)
// [
// Contact(firstName: "Charles", lastName: "Alexson"),
// Contact(firstName: "Leonard", lastName: "Charleson"),
// Contact(firstName: "Alex", lastName: "Elexson"),
// Contact(firstName: "Michael", lastName: "Elexson"),
// Contact(firstName: "Michael", lastName: "Webb")
// ]
用于对数组进行排序的公式
var students = ["Kofi", "Abena", "Peter", "Kweku", "Akosua"]
students.sort()
print(students)
// Prints ["Abena", "Akosua", "Kofi", "Kweku", "Peter"]
快乐编码...:)
答案 1 :(得分:0)
代码中有很多问题。
首先使用struct
struct Student {
let name : String
let id : Int
}
然后声明两个 native 数组(从不使用NSMutable..
集合类型),一个数组用于完整的数据集,另一个数组用于过滤的数据
var students = [Student(name:"Rancho", id: 1437404654),
Student(name:"Mastora", id: 1932617669),
Student(name:"Amanda", id: 1457367179)]
var filteredStudents = [Student]()
在textDidChange
中检查空字符串,过滤数据并相应地设置searchActive
。
再一次,与Foundation NSPredicate
相比,首选本机筛选器API
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchText.isEmpty {
searchActive = false
filteredStudents.removeAll()
} else {
filteredStudents = students.filter {$0.name.range(of: searchText, options: [.caseInsensitive, .diacriticInsensitive]) != nil
|| String($0.id).range(of: searchText) != nil }
searchActive = true
}
resultTable.reloadData()
}
过滤后的数组与主数组的顺序相同。
在表格视图中,数据源方法根据searchActive
来显示数据
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searchActive ? filteredStudents.count : students.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyCell
let student = searchActive ? filteredStudents[indexPath.row] : students[indexPath.row]
cell.idTextField.text = "\(student.id)"
cell.nameTextField.text = student.name"
return cell
}