Ios 滚动时,UISwitch正在更改其他单元格上的状态
我的tableview看起来像这样Ios 滚动时,UISwitch正在更改其他单元格上的状态,ios,swift,uitableview,uiswitch,Ios,Swift,Uitableview,Uiswitch,我的tableview看起来像这样 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! Cell return cell } func tableView(_ t
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! Cell
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 200
}
class Cell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
我的tableviewcell看起来像这样
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! Cell
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 200
}
class Cell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
当我改变任何单元格上的开关状态并滚动时。另一个单元格的开关周期性地改变了其他单元格的滚动状态。请帮忙
为中的每个单元格正确处理开关控制
tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
因此,当单元出列时,它会正确地处理每个单元的开关控制
解决方案:
当您更改单元格的开关控制状态时,请将该indexPath
保存在某个位置,以便在tableviewcellForRowAt indexPath
中迭代此indexPath时,再次设置该开关状态,否则设置默认状态为中的每个单元格正确处理开关控制
tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
因此,当单元出列时,它会正确地处理每个单元的开关控制
解决方案:
当您更改单元格的开关控制状态时,将该索引保存在某个地方,这样当您在tableviewcellForRowAt indexPath
中迭代此indexPath时,您可以再次设置开关的状态,否则设置默认状态您需要为每个开关设置带有状态的数据数组,并在didSelect方法中更改开关的数据模型,然后在get的cellForRow中从阵列切换状态,并且您的viewController需要为SwitchControlDelegate
class UIViewController:SwitchControlDelegate{
var array = [Bool](count: 200, repeatedValue:false)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! Cell
cell.switchControl.isOn = array[indexPath.row]
cell.switchDelegate = self
cell.index = indexPath.row
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 200
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
array[indexPath.row] = !array[indexPath.row]
}
func switchChangeStateWithIndex(index:Int){
array[index] = !array[index]
}
这将在您单击单元格时更改开关状态,如果您需要在更改开关控件时更改状态,则需要委托更新viewController中的数据模型
在您的单元格文件中:
protocol SwitchControlDelegate: class {
func switchChangeStateWithIndex(index:Int)
}
class Cell: UITableViewCell {
var index:Int = 0
weak var switchDelegate: SwitchControlDelegate?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func valueChanged(_ sender: AnyObject) {
switchDelegate?.switchChangeStateWithIndex(index:index)
}
}
您需要将开关控件的动作值更改与此功能值更改相匹配,因此,每次更改开关状态时,单元格需要调用func value CHANGED,您需要为每个开关设置带有状态的数据数组,并在didSelect方法中更改开关的数据模型,然后在单元格FORROW中从数组获取开关状态,并且您的viewController需要是SwitchControlDelegate
class UIViewController:SwitchControlDelegate{
var array = [Bool](count: 200, repeatedValue:false)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! Cell
cell.switchControl.isOn = array[indexPath.row]
cell.switchDelegate = self
cell.index = indexPath.row
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 200
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
array[indexPath.row] = !array[indexPath.row]
}
func switchChangeStateWithIndex(index:Int){
array[index] = !array[index]
}
这将在您单击单元格时更改开关状态,如果您需要在更改开关控件时更改状态,则需要委托更新viewController中的数据模型
在您的单元格文件中:
protocol SwitchControlDelegate: class {
func switchChangeStateWithIndex(index:Int)
}
class Cell: UITableViewCell {
var index:Int = 0
weak var switchDelegate: SwitchControlDelegate?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func valueChanged(_ sender: AnyObject) {
switchDelegate?.switchChangeStateWithIndex(index:index)
}
}
您需要将开关控件中的操作值更改与此函数值更改相匹配,因此每次更改开关状态时,单元格都需要调用func value CHANGED这是因为单元格退出队列。你能用数据模型更新你的问题吗?这就是问题所在。我创建了测试项目,以查看是否是我的数据模型导致了此问题。但仅使用这些代码,它就给了我同样的问题请检查我的更新在数据模型中添加一个布尔标志(用于加载传感器数据),并在更新时保存开关状态。在cellForRowAt方法中,根据布尔标志相应地设置开关状态。这是因为单元出列。你能用数据模型更新你的问题吗?这就是问题所在。我创建了测试项目,以查看是否是我的数据模型导致了此问题。但仅使用这些代码,它就给了我同样的问题请检查我的更新在数据模型中添加一个布尔标志(用于加载传感器数据),并在更新时保存开关状态。在cellForRowAt方法中,根据布尔标志相应地设置开关状态。嗨,Svetoslav,我对您的代码也有同样的问题。当我滚动时,切换为的选定行将发生更改。返回未选中状态。请查看我的更新代码,如果仍然存在问题,请将viewController和cellFile与所有iOutlet和iBactionsHanks一起粘贴给我,以获取新代码,如果不起作用,将发布。但你能告诉cell.delegate吗。它显示单元格没有委托方法的错误。请帮助修复此问题以测试您的代码我已设法修复此问题。附加屏幕显示与我的问题的连接您需要匹配操作Hi Svetoslav,我对您的代码有相同的问题。当我滚动时,切换为的选定行将发生更改。返回未选中状态。请查看我的更新代码,如果仍然存在问题,请将viewController和cellFile与所有iOutlet和iBactionsHanks一起粘贴给我,以获取新代码,如果不起作用,将发布。但你能告诉cell.delegate吗。它显示单元格没有委托方法的错误。请帮助修复此问题以测试您的代码我已设法修复此问题。附加屏幕,显示与我的问题的连接,您需要匹配操作