gpt4 book ai didi

ios - 即使采取措施后,UICollectionView 单元格重用也会导致问题

转载 作者:行者123 更新时间:2023-11-29 01:19:50 24 4
gpt4 key购买 nike

我知道这个问题已经被问过很多次了。

我正在使用带有自定义单元格的 UICollectionView,这些单元格具有一些属性,最重要的是 UISwitch 数组和 UILabel< 数组 的。您可以猜到,当我滚动时,标签重叠并且开关改变状态。我已经实现了 UICollectionViewCell prepareForReuse 的方法,其中我清空了这些数组并重置了主标签文本。

我尝试结合不同答案的解决方案,并且已经达到了保留标签的地步,但单元格中开关的状态却没有。我的下一步是创建一个数组以在删除开关之前保留状态,然后将新创建的开关的 on 属性设置为该数组在索引处的值。这行得通,直到我快速滚动并在以前未选择的单元格中切换后才被选中(或未选中)。这给我带来了很大的问题。

这是我的 collectionView cellForItemAtIndexPath 方法:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("pitanjeCell", forIndexPath: indexPath) as! PitanjeCell

// remove views previously created and create array to preserve the state of switches
var selectedSwitches: [Bool] = []

for item: UIView in cell.contentView.subviews {
if (item.isKindOfClass(UILabel) && !item.isEqual(cell.tekstPitanjaLabel)){
item.removeFromSuperview()
}
if (item.isKindOfClass(UISwitch)){
selectedSwitches.append((item as! UISwitch).on)
item.removeFromSuperview()
}
}

// get relevant data needed to place cells programmatically
cell.tekstPitanjaLabel.text = _pitanja[indexPath.row].getText()
let numberOfLines: CGFloat = CGFloat(cell.tekstPitanjaLabel.numberOfLines)
cell.setOdgovori(_pitanja[indexPath.row].getOdgovori() as! [Odgovor])
var currIndex: CGFloat = 1
let floatCount: CGFloat = CGFloat(_pitanja.count)
let switchConstant: CGFloat = 0.8
let switchWidth: CGFloat = cell.frame.size.width * 0.18
let heightConstant: CGFloat = (cell.frame.size.height / (floatCount + 2) + (numberOfLines * 4))
let labelWidth: CGFloat = cell.frame.size.width * 0.9

for item in _pitanja[indexPath.row].getOdgovori() {

// create a switch
let odgovorSwitch: UISwitch = UISwitch(frame: CGRectMake((self.view.frame.size.width - (switchWidth * 2)), currIndex * heightConstant , switchWidth, 10))
odgovorSwitch.transform = CGAffineTransformMakeScale(switchConstant, switchConstant)
let switchValue: Bool = selectedSwitches.count > 0 ? selectedSwitches[Int(currIndex) - 1] : false
odgovorSwitch.setOn(switchValue, animated: false)

// cast current item to relevant class
let obj: Odgovor = item as! Odgovor

// create a label
let odgovorLabel: UILabel = UILabel(frame: CGRectMake((self.view.frame.size.width / 12), currIndex * heightConstant , labelWidth, 20))
odgovorLabel.text = obj.getText();
odgovorLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
odgovorLabel.font = UIFont(name: (odgovorLabel.font?.fontName)!, size: 15)

// add to cell
cell.addSwitch(odgovorSwitch)
cell.addLabel(odgovorLabel)

currIndex++
}

return cell
}

我的自定义单元格还实现了 addSwitchaddLabel 方法,它们将元素作为 subview 添加到 contentView 中.

有什么方法可以在滚动时始终保持开关的状态?

编辑:根据@Victor Sigler 的建议,我创建了一个像这样的按维数组:

var _switchStates: [[Bool]]!

我是这样初始化的:

let odgovori: Int = _pitanja[0].getOdgovori().count
_switchStates = [[Bool]](count: _pitanja.count, repeatedValue: [Bool](count: odgovori, repeatedValue: false))

然后我改变了我的方法:

for item: UIView in cell.contentView.subviews {
if (item.isKindOfClass(UILabel) && !item.isEqual(cell.tekstPitanjaLabel)){
item.removeFromSuperview()
}
if (item.isKindOfClass(UISwitch)){
_switchStates[indexPath.row][current] = (item as! UISwitch).on
current++
selectedSwitches.append((item as! UISwitch).on)
item.removeFromSuperview()
}
}

最后:

 let switchValue: Bool = _switchStates.count > 0 ? _switchStates[indexPath.row][Int(currIndex) - 1] : false

最佳答案

首先,正如您在关于 UICollectionView 中单元格的默认行为的问题中所说的那样您需要保存每个单元格的状态,在您的情况下是 UISwitch ,但您需要在本地属性中保留状态,而不是在 cellForRowAtIndexPath 中方法,因为每次要重用单元格时都会调用此方法。

所以首先声明要保存 UISwitch 状态的数组在这个函数之外,像这样:

var selectedSwitches: [Bool] = []

或者您可以声明它,然后在您的 viewDidLoad 中实例化它,这取决于你,我建议你在你的 viewDidLoad 中实例化它并且只将它声明为这样的属性:

var selectedSwitches: [Bool]!

然后你可以用你的 UISwitch 的状态做任何你想做的事当更改为 on 时,当然总是保留或 off .

希望对你有帮助

关于ios - 即使采取措施后,UICollectionView 单元格重用也会导致问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34751508/

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