我有一个带有2个按钮(活动和不活动)的视图,以及一个显示所有用户的表格视图。我想如果用户使用显示活动/不活动用户单击按钮之一,如何加载相关数据?
import UIKit
class ViewController: UIViewController,UITableViewDataSource, UISearchBarDelegate {
var users:[User] = []
let user1 = User(username: "user1",email: "user1@mail.com",active: true)
let user2 = User(username: "user2",email: "user2@mail.com",active: true)
let user3 = User(username: "user3",email: "user3@mail.com",active: false)
@IBOutlet weak var btnActiveUser: UIButton!
@IBOutlet weak var btnInActiveUser: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
users.append(user1)
users.append(user2)
users.append(user3)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return users.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "this cell index for \(users[indexPath.row].username) "
return cell
}
@IBAction func ActionShowActiveUsers(_ sender: Any) {
let activeUser = users.filter( { return $0.active == true } )
print(activeUser.count)
}
@IBAction func btnShowInActiveUser(_ sender: Any) {
let inactiveUser = users.filter( { return $0.active == false } )
print(inactiveUser.count)
}
}
答案 0 :(得分:3)
您需要
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 {$0.active}
print(currentUsers.count)
tableView.reloadData()
}
@IBAction func btnShowInActiveUser(_ sender: Any) {
currentUsers = users.filter { !$0.active }
print(currentUsers.count)
tableView.reloadData()
}
在viewDidLoad
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
答案 1 :(得分:1)
首先,因为您不使用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 { $0.active }
tableView.reloadData()
}
@IBAction func btnShowInActiveUser(_ sender: Any) {
filteredUsers = users.filter { !$0.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
}
答案 2 :(得分:1)
您可以有一个包含所有用户的主数组,并使用另一个数组作为dataSource
的{{1}}:
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
}
答案 3 :(得分:1)
首先,您需要几个新变量:
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 $0.active == true } )
print(activeUser.count)
filterType = .active
tableView.reloadData()
}
@IBAction func btnShowInActiveUser(_ sender: Any) {
let inactiveUser = users.filter( { return $0.active == false } )
print(inactiveUser.count)
filterType = .inactive
tableView.reloadData()
}