[英]UICollectionView scrolling changes the selected cells
我有一個UICollectionView帶有圖像作為單元格內容。 選擇單元格時,我需要可見selectionView。 (在第一個水龍頭選擇視圖上可見,在下一個水龍頭選擇視圖上隱藏)。 我把代碼放在這里
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if(selectionFlag){
deleteButton.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
deleteButton.enabled = true
addButton.enabled = true
addButton.setTitle("Add to", forState: .Normal)
let cell = channelItemCollectionView.cellForItemAtIndexPath(indexPath) as! ChannelItemListCollectionViewCell
cell.selectionView.alpha = 0.4
cell.tickButton.frame = CGRect(x: ((UIScreen.mainScreen().bounds.width/3)-2) - 25, y: 3, width: 20, height: 20)
if imageDataSource[indexPath.row][selectionKey] as! String == "0"
{
//selected
imageDataSource[indexPath.row][selectionKey] = "1"
cell.selectionView.hidden = false
selectedArray.append([selectionKey:"1", mediaIdKey:String(indexPath.row)])
cell.insertSubview(cell.selectionView, aboveSubview: cell.channelItemImageView)
}
else
{
//deselected
imageDataSource[indexPath.row][selectionKey] = "0"
cell.selectionView.hidden = true
selectedArray.append([selectionKey:"0", mediaIdKey:String(indexPath.row)])
cell.insertSubview(cell.channelItemImageView, aboveSubview: cell.selectionView)
}
}
}
並且cellforItematindex為:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ChannelItemListCollectionViewCell.identifier, forIndexPath: indexPath) as! ChannelItemListCollectionViewCell
collectionView.allowsMultipleSelection = true
if(selectionFlag){
if(selectedArray.count > 0)
{
for element in selectedArray{
if element[mediaIdKey] as? String == imageDataSource[indexPath.row][mediaIdKey] as? String
{
if element[selectionKey] as! String == "1"
{
imageDataSource[indexPath.row][selectionKey] = "1"
mediaSelected.setValue(element[mediaIdKey]!, forKey: String(indexPath.row))
}
else{
imageDataSource[indexPath.row][selectionKey] = "0"
mediaSelected.removeObjectForKey(String(indexPath.row))
}
}
}
}
}
else{
cell.selectionView.hidden = true
}
if imageDataSource.count > 0
{
let imageData = imageDataSource[indexPath.row][mediaUrlKey] as! NSData
cell.channelItemImageView.image = UIImage(data: imageData)
}
cell.tickButton.frame = CGRect(x: ((UIScreen.mainScreen().bounds.width/3)-2) - 25, y: 3, width: 20, height: 20)
return cell
}
這里選擇了單元,selectedArray存儲了索引,但是沒有重新加載單元時間。 請幫我?
按照您當前的實現,在單元格cellForItemAtIndexPath
您還應該設置cell.selectionView.hidden = true/false
if element[selectionKey] as! String == "1"
{
cell.selectionView.hidden = false // show selection
imageDataSource[indexPath.row][selectionKey] = "1"
mediaSelected.setValue(element[mediaIdKey]!, forKey: String(indexPath.row))
}
else{
cell.selectionView.hidden = true // hide selection
imageDataSource[indexPath.row][selectionKey] = "0"
mediaSelected.removeObjectForKey(String(indexPath.row))
}
調用超prepareForReuse
在您單元的prepareForReuse方法方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.