Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 从侧菜单用户类型中删除索引_Swift_Uitableview_Didselectrowatindexpath - Fatal编程技术网

Swift 从侧菜单用户类型中删除索引

Swift 从侧菜单用户类型中删除索引,swift,uitableview,didselectrowatindexpath,Swift,Uitableview,Didselectrowatindexpath,我已经在tableview中实现了侧菜单,现在我的场景是,我必须以用户类型管理侧菜单选项 让我看看我的代码 var items = ["Social Media Post", "Messages", "Manage User","My Account","Information","Logout"] func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

我已经在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方法中更改数组,这样做不会得到想要的结果。从协议方法重写之外调整数组,只需调用reloadData()方法

这会起作用,但你在这里犯了一个小错误

设置标签后,您已从阵列中删除。所以您需要先从数组中删除该项,然后再设置标签

顺便说一句,我不推荐这种方法,因为您需要为每个
cellForRowAt
方法添加/删除数组

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
}