两个部分与一个 uiswitch? - Swift
Two sections with one uiswitch? - Swift
我有一个工作开关和一个开关状态,它使用索引路径的行号和存储在字典中的布尔值来跟踪打开哪个开关。虽然这仅适用于一个部分。我很难阻止它溢出到下一部分,如下所示:
Section 0 Row 3 switch turned on
Section 1 Row 3 switch turned on without me pressing it.
有没有办法只为那个特定的部分保持开关?现在我正在使用两个原型单元格,一个用于我正在显示的数据,其中仅包含一个开关,另一个单元格用于显示 header.
部分
以下是一些我认为有助于查看我放下的内容的代码:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("advCell", forIndexPath: indexPath) as! advDataCell
cell.advDelegate = self
switch(indexPath.section) {
case 0:
cell.lblCategoryItem.text = foodCategories[indexPath.row]["name"]
case 1:
cell.lblCategoryItem.text = activitiesCategories[indexPath.row]["name"]
default:
return cell
}
if advSwitchStates[indexPath.row] != nil {
cell.advOnOffSwitch.on = advSwitchStates[indexPath.row]!
}
else {
cell.advOnOffSwitch.on = false
}
cell.advOnOffSwitch.on = advSwitchStates[indexPath.row] ?? false
return cell
}
func switchCell(advSwitchCell: advDataCell,didChangeValue value: Bool) {
let indexPath = tableView.indexPathForCell(advSwitchCell)!
print("This advanced filter controller has received the switch event.")
advSwitchStates[indexPath.row] = value
}
以及我用来存储开关状态的内容:
var advSwitchStates = [Int: Bool]()
在您的 cellForRowAtIndexPath
中,您将回收的单元出列,就像您应该做的那样。您需要完全配置单元格中的每个视图,在所有情况下。这意味着在所有情况下,您都需要为 advOnOffSwitch
设置一个值。
在您的 cellForRowAtIndexPath
方法中,您有一个针对 0、1 或任何其他值的节值的 switch 语句。如果 section 值不是 0 或 1,则您 return 没有设置 advOnOffSwitch
的状态。如果您回收一个已设置 advOnOffSwitch
的单元格,它将保持打开状态,这是您不想要的。像这样更改您的 switch 语句:
switch(indexPath.section) {
case 0:
cell.lblCategoryItem.text = foodCategories[indexPath.row]["name"]
case 1:
cell.lblCategoryItem.text = activitiesCategories[indexPath.row]["name"]
default:
cell.advOnOffSwitch.on = false
return cell
}
使用该代码,您可以强制将开关置于关闭位置以启动第 0 或 1 部分以外的部分。
有两个不同的问题,首先在设置单元格名称时去掉前面的return:
switch(indexPath.section) {
case 0:
cell.lblCategoryItem.text = foodCategories[indexPath.row]["name"]
default:
cell.lblCategoryItem.text = activitiesCategories[indexPath.row]["name"]
}
其次,由于您有多个部分,因此您需要使用部分和行作为键来跟踪切换状态。最简单的方法可能是将 advSwitchStates 的键设为 NSIndexPath。
例如声明为:
var advSwitchStates = [NSIndexPath: Bool]()
然后在 cellForRowAtIndexPath
cell.advOnOffSwitch.on = advSwitchStates[indexPath] ?? false
我有一个工作开关和一个开关状态,它使用索引路径的行号和存储在字典中的布尔值来跟踪打开哪个开关。虽然这仅适用于一个部分。我很难阻止它溢出到下一部分,如下所示:
Section 0 Row 3 switch turned on
Section 1 Row 3 switch turned on without me pressing it.
有没有办法只为那个特定的部分保持开关?现在我正在使用两个原型单元格,一个用于我正在显示的数据,其中仅包含一个开关,另一个单元格用于显示 header.
部分以下是一些我认为有助于查看我放下的内容的代码:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("advCell", forIndexPath: indexPath) as! advDataCell
cell.advDelegate = self
switch(indexPath.section) {
case 0:
cell.lblCategoryItem.text = foodCategories[indexPath.row]["name"]
case 1:
cell.lblCategoryItem.text = activitiesCategories[indexPath.row]["name"]
default:
return cell
}
if advSwitchStates[indexPath.row] != nil {
cell.advOnOffSwitch.on = advSwitchStates[indexPath.row]!
}
else {
cell.advOnOffSwitch.on = false
}
cell.advOnOffSwitch.on = advSwitchStates[indexPath.row] ?? false
return cell
}
func switchCell(advSwitchCell: advDataCell,didChangeValue value: Bool) {
let indexPath = tableView.indexPathForCell(advSwitchCell)!
print("This advanced filter controller has received the switch event.")
advSwitchStates[indexPath.row] = value
}
以及我用来存储开关状态的内容:
var advSwitchStates = [Int: Bool]()
在您的 cellForRowAtIndexPath
中,您将回收的单元出列,就像您应该做的那样。您需要完全配置单元格中的每个视图,在所有情况下。这意味着在所有情况下,您都需要为 advOnOffSwitch
设置一个值。
在您的 cellForRowAtIndexPath
方法中,您有一个针对 0、1 或任何其他值的节值的 switch 语句。如果 section 值不是 0 或 1,则您 return 没有设置 advOnOffSwitch
的状态。如果您回收一个已设置 advOnOffSwitch
的单元格,它将保持打开状态,这是您不想要的。像这样更改您的 switch 语句:
switch(indexPath.section) {
case 0:
cell.lblCategoryItem.text = foodCategories[indexPath.row]["name"]
case 1:
cell.lblCategoryItem.text = activitiesCategories[indexPath.row]["name"]
default:
cell.advOnOffSwitch.on = false
return cell
}
使用该代码,您可以强制将开关置于关闭位置以启动第 0 或 1 部分以外的部分。
有两个不同的问题,首先在设置单元格名称时去掉前面的return:
switch(indexPath.section) {
case 0:
cell.lblCategoryItem.text = foodCategories[indexPath.row]["name"]
default:
cell.lblCategoryItem.text = activitiesCategories[indexPath.row]["name"]
}
其次,由于您有多个部分,因此您需要使用部分和行作为键来跟踪切换状态。最简单的方法可能是将 advSwitchStates 的键设为 NSIndexPath。
例如声明为:
var advSwitchStates = [NSIndexPath: Bool]()
然后在 cellForRowAtIndexPath
cell.advOnOffSwitch.on = advSwitchStates[indexPath] ?? false