【发布时间】:2018-08-13 09:47:57
【问题描述】:
我的设置 VC 中有一个简单的表格视图。有两种不同的情况,登录用户是员工或管理员。管理员能够将用户添加到组织中,因此他看到两个部分,每个部分(添加用户并注销),而普通员工只能看到一个部分,每行(注销)。为了管理 indexPaths,我有这些结构:
struct Indexes {
struct AddEmployee {
static let row = 0
static let section = isEmployee() ? -1 : 0
static func indexPath() -> IndexPath {
return IndexPath(row: row, section: section)
}
}
struct SignOut {
static let row = 0
static let section = isEmployee() ? 0 : 1
static func indexPath() -> IndexPath {
return IndexPath(row: row, section: section)
}
}
}
在我的 TableView 委托中,我得到了这些方法:
func numberOfSections(in tableView: UITableView) -> Int {
return isEmployee() ? 1 : 2
}
和
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SettingsTableViewCell", for: indexPath)
if (indexPath == Indexes.AddEmployee.indexPath()) {
cell.textLabel?.text = "Add Employee"
} else if (indexPath == Indexes.SignOut.indexPath()) {
cell.textLabel?.text = "Log out"
}
return cell
}
现在的问题是,当我以 Admin 身份退出并以 Employee 身份登录(反之亦然)时,结构的 static 变量仍处于初始化状态,而 isEmployee() 返回值已更改的事实并没有没关系,因为变量没有重新加载,所以我的单元格数量错误。是否有可能强制结构重新加载其静态变量?
【问题讨论】:
标签: swift