UICollectionView滚动更改选定的单元格

时间:2016-04-04 12:08:08

标签: ios swift uicollectionview

我有一个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存储索引,但重新加载单元格未加载的时间。请帮帮我?

2 个答案:

答案 0 :(得分:1)

根据您当前的实施方式,在单元格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))
  }

答案 1 :(得分:1)

在单元格的prepareForReuse方法中调用super listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { TextView tv = (TextView) view.findViewById(R.id.txt_expenses_details); tv.setText(...); customListAdapter.notifyDataSetChanged(); } }); 方法。