gpt4 book ai didi

ios - 如何在用户点击 Ui 按钮时将相关数据加载到表中?

转载 作者:行者123 更新时间:2023-11-28 09:39:17 24 4
gpt4 key购买 nike

我有一个带有 2 个按钮(事件和非事件)的 View 和一个 TableView 来显示我的所有用户。我想如果用户使用显示事件/非事件用户单击其中一个按钮,我该如何加载相关数据?

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)
}
}

最佳答案

你需要

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")

关于ios - 如何在用户点击 Ui 按钮时将相关数据加载到表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54220489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com