gpt4 book ai didi

swift - 明智地从侧边菜单用户类型中删除索引

转载 作者:行者123 更新时间:2023-11-28 14:10:16 24 4
gpt4 key购买 nike

我已经在 tableview 中实现了侧边菜单,现在我的场景是,我必须将侧边菜单选项作为用户类型来管理

让我展示一下我的代码

var items = ["Social Media Post", "Messages", "Manage User","My Account","Information","Logout"]



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! MenuTableViewCell
cell.lblTitle.text = items[indexPath.row]
cell.imgMenuLogo.image = image[indexPath.row]
print(User_type)
if User_type == 2{
items.remove(at: 0)
}
return cell
}

但现在我想要。像这样去

   if user_type == "3"{
// Social Media , Messages And Manage User options i want to remove

}

我无法理解如何从索引中删除。

最佳答案

尝试这样的事情:

override func viewDidLoad() {
super.viewDidLoad()
getList()
}

func getList(){
switch userType{
case 0:
items = ["UserTypeOne_Home","UserType_One Settings","etc"]
break
case 1:
items = ["UserTypeTwo_Home","UserType_Two Settings","etc"]
break
default:
break
}
self.tableView.reloadData()
}


extension ViewController: UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "Some ID")
cell?.textLabel?.text = items[indexPath.row]
return cell!
}
}

尽量不要使用 indexPath.row 从 cellForRowAt indexPath 方法中更改数组,这不会给您想要的结果。从协议(protocol)方法覆盖之外调制数组,只需调用 reloadData() 方法即可。

关于swift - 明智地从侧边菜单用户类型中删除索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52620250/

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