gpt4 book ai didi

ios - TableView 行隐藏和显示为 Bool 函数

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

如图所示,最初我需要在用户填写通过“成为商家”行访问的表单时隐藏 TableView 中的最后 2 行(我的个人资料和更新个人资料)。我需要隐藏“成为商家”行并显示最后两行。这是通过 bool 函数 (true/false) 完成的。

check image

我该怎么做?

var arrdata = ["Home", "Orders", "Change Password", "Log Out", "Become A Merchant", "My Profile", "Update Profile"]

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var isMerchantProfile: Bool = UserDefaults.standard.bool(forKey: "isFillMerchant")
if isMerchantProfile == true {
return 4
}

return arrdata.count
}

最佳答案

从数据数组中删除/添加要显示的行,然后使用 TableView 函数 insertRowsAt/deleteRowsAt。使用 performBatchUpdates 函数进行批量更新。

引用UITableView documentation .

在这种情况下,我还建议使用 enum 来定义您的行,而不是代表行标题的原始字符串。

例如(在你的 View Controller 类中没问题)

enum Row {
case home, orders, changePassword, logout, becomeMerchant, myProfile, updateProfile

var title: String {
switch self {
case .home:
return NSLocalizedString("Home", comment: "Row title")
// … etc
}
}
}

这样就可以更轻松地找到要删除的行/插入行的位置并保持代码整洁。

关于ios - TableView 行隐藏和显示为 Bool 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53415247/

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