我的情况是,我正在使用具有两个部分的tableview。第一部分具有一行,另一部分具有五行。在这里,我正在从数组加载数据。我需要更新和替换数组中的值,还需要显示和隐藏节中的特定行。
var settings_sections = ["Account", "General Settings"]
var settings_title = [[“User Account"], ["Contact Us", "Share App", "Terms","Privacy", "Logout"]]
let settings_icon = [[UIImage(named: "account.png")], [UIImage(named: "contactus.png"), UIImage(named: "share.png"), UIImage(named: "terms.png"), UIImage(named: "privacy.png”), UIImage(named: "logout.png")]]
在“帐户”部分,我需要将“用户帐户”初始值更新为我的名字,并需要显示和隐藏“注销”行。
答案 0 :(得分:1)
您可以更改数组元素并调用[reloadData()] [1]
var settings_title = [[“User Account"], ["Contact Us", "Share App", "Terms","Privacy", "Logout"]]
settings_title[0][0] = "Your name"
tableView.reloadData()
并且不要在变量声明中使用下划线
使用settingsTitle
代替settings_title
//Hide logout
if var general = settings_title.last {
if let logoutIndex = general.firstIndex(of: "Logout") {
general.remove(at: logoutIndex)
}
settings_title[1] = general
}
tableView.reloadData()
//show logout
if var general = settings_title.last {
general.append("Logout")
settings_title[1] = general
}
tableView.reloadData()
使用array
的结构而不是创建多个数组。
class MasterViewController: UITableViewController {
struct Menu {
var title: String
var icon: UIImage?
}
struct Section {
var sectionTitle: String
var menus: [Menu]
}
var settingsSections = [Section(sectionTitle: "Account", menus: [Menu(title: "User Account", icon: UIImage(named: "account"))]),
Section(sectionTitle: "General Settings", menus:
[Menu(title: "Contact Us", icon: UIImage(named: "contactus")), Menu(title: "Share App", icon: UIImage(named: "share")),
Menu(title: "Terms", icon: UIImage(named: "terms")), Menu(title: "Privacy", icon: UIImage(named: "privacy")),
Menu(title: "Logout", icon: UIImage(named: "logout"))])]
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
self.changeName()
}
DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) {
self.hideLogout()
}
DispatchQueue.main.asyncAfter(deadline: .now() + 20.0) {
self.showLogout()
}
DispatchQueue.main.asyncAfter(deadline: .now() + 20.0) {
self.showLogout()
}
DispatchQueue.main.asyncAfter(deadline: .now() + 20.0) {
self.showLogout()
}
}
func changeName() {
if let account = settingsSections.first, let userAccount = account.menus.first {
var newUser = userAccount
newUser.title = "Your name"
settingsSections[0].menus[0] = newUser
}
tableView.reloadData()
}
func hideLogout() {
if let general = settingsSections.last {
let newMenus = general.menus.filter { $0.title != "Logout" }
settingsSections[1].menus = newMenus
}
tableView.reloadData()
}
func showLogout() {
if let general = settingsSections.last, !general.menus.contains(where: { $0.title == "Logout" }) {
var newMenus = general.menus
newMenus.append(Menu(title: "Logout", icon: UIImage(named: "logout")))
settingsSections[1].menus = newMenus
}
tableView.reloadData()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return settingsSections.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return settingsSections[section].sectionTitle
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return settingsSections[section].menus.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") ?? UITableViewCell(style: .default, reuseIdentifier: "Cell")
let menu = settingsSections[indexPath.section].menus[indexPath.row]
cell.textLabel?.text = menu.title
cell.imageView?.image = menu.icon
return cell
}
}