в facebook невозможно получить пол профиля, но вы можете разделить имя и отправить запрос на https://genderapi.io
https://genderapi.io/api/?name=mike
blockquote>
{"name":"mike", "q":"mike", "gender":"male", "total_names":8336, "probability":99}
Вы можете иметь основной массив, который содержит всех пользователей, и использовать другой массив в качестве dataSource
вашего tableView
:
var mainUsers: [User] = []
var userDataSource: [User] = []
В viewDidLoad
:
override func viewDidLoad() {
super.viewDidLoad()
mainUsers.append(user1)
mainUsers.append(user2)
mainUsers.append(user3)
userDataSource = mainUsers
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return userDataSource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "this cell index for \(userDataSource[indexPath.row].username) "
return cell
}
Тогда в ваших кнопках действий:
@IBAction func ActionShowActiveUsers(_ sender: Any) {
userDataSource = mainUsers.filter( { return [112].active == true } )
tableView.reloadData()
}
@IBAction func btnShowInActiveUser(_ sender: Any) {
userDataSource = mainUsers.filter( { return [112].active == false } )
tableView.reloadData()
}
Вам нужно
var allUsers = [User]()
var currentUsers = [User]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return currentUsers.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
cell.textLabel?.text = "this cell index for \(currentUsers[indexPath.row].username) "
return cell
}
@IBAction func ActionShowActiveUsers(_ sender: Any) {
currentUsers = users.filter {[110].active}
print(currentUsers.count)
tableView.reloadData()
}
@IBAction func btnShowInActiveUser(_ sender: Any) {
currentUsers = users.filter { ![110].active }
print(currentUsers.count)
tableView.reloadData()
}
Добавить это в viewDidLoad
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
Прежде всего вам нужно несколько новых переменных:
enum FilterType { // you can add here any filter options you want
case allUsers, active, inactive
}
var users = [User]()
var activeUsers = [User]()
var inactiveUsers = [User]()
var filterType = FilterType.allUsers
После этого отредактируйте метод numberOfRowInSection
:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch filterType {
case .allUsers:
return users
case .active:
return activeUsers
case .inactive:
return inactiveUsers
}
}
И не забудьте добавить tableView.reloadData()
в конец действия вашей кнопки:
@IBAction func ActionShowActiveUsers(_ sender: Any) {
let activeUser = users.filter( { return [112].active == true } )
print(activeUser.count)
filterType = .active
tableView.reloadData()
}
@IBAction func btnShowInActiveUser(_ sender: Any) {
let inactiveUser = users.filter( { return [112].active == false } )
print(inactiveUser.count)
filterType = .inactive
tableView.reloadData()
}
Во-первых, вам нужен выход для представления таблицы, поскольку вы не используете UITableViewController
@IBOutlet weak var tableView: UITableView!
, а затем внутри viewDidLoad
задаете его делегата и источник данных в качестве контроллера представления
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
@IBOutlet weak var tableView: UITableView!
...
override func viewDidLoad() {
super.viewDidLoad()
...
tableView.delegate = self
tableView.dataSource = self
}
...
}
Вы можете иметь два массива. Один для всех пользователей и один для отфильтрованных пользователей по вашему состоянию (активно или нет)
var users = [User]()
var filteredUsers: [User] = users /* or just some other default value, e.g empty array */
Этот отфильтрованный массив вы можете назначить фильтрованным массивом каждый раз, когда вы нажимаете определенную кнопку и не забудьте перезагрузить данные то ваше табличное представление затем
@IBAction func ActionShowActiveUsers(_ sender: Any) {
filteredUsers = users.filter { [113].active }
tableView.reloadData()
}
@IBAction func btnShowInActiveUser(_ sender: Any) {
filteredUsers = users.filter { ![113].active }
tableView.reloadData()
}
затем в качестве массива источника данных для вашего табличного представления используйте filteredUsers
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredUsers.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "this cell index for \(filteredUsers[indexPath.row].username)"
return cell
}