应在集合视图中的每个单元格选择/取消选择中插入/删除Tickmark图像

时间:2016-10-18 11:33:34

标签: ios iphone swift

我启用了多项选择。当我点击它时,我正在成功更改单元格的颜色。但是我还想在选中时为单元格添加一个tickmark图像,并在取消选择时删除此图像我是swift编程的新手,请解决这个问题。我的集合查看委托和数据源

func collectionView(collectionView: UICollectionView,    numberOfItemsInSection section: Int) -> Int
{
 return marrCategoriesInfo.count
}

 func collectionView(collectionView: UICollectionView,   cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell = collectionVieww   .dequeueReusableCellWithReuseIdentifier("myCell", forIndexPath:    indexPath)as! CollectionViewCell
    for _ in 0 ..< marrCategoriesInfo.count  {
         cell.backgroundColor =   self.colorWithHexString(arrColors[indexPath.row])
         if (arrColors.count < marrCategoriesInfo.count)
         {
             arrColors.appendContentsOf(arrColors)
        }
    }



    cell.labellpersons.text = marrCategoriesInfo[indexPath.row] as?  String
    cell.labellpersons.textColor = UIColor .whiteColor()

       element = self.marrCategoriesInfo[indexPath.row] as! NSString
    if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad
    {
        stringSize = element.sizeWithAttributes([NSFontAttributeName:  UIFont.systemFontOfSize(25)]) as CGSize
        cell.labellpersons.sizeThatFits(stringSize)
        cell.layer.borderWidth = 2;
        cell.layer.borderColor = UIColor .blackColor().CGColor
        cell.layer.cornerRadius = 35;
        cell.labellpersons.textAlignment = .Center
    }
    else
    {
        stringSize = element.sizeWithAttributes([NSFontAttributeName:  UIFont.systemFontOfSize(12.0)]) as CGSize
        cell.labellpersons.sizeThatFits(stringSize)
        cell.layer.borderWidth = 1;
        cell.layer.borderColor = UIColor .blackColor().CGColor
        cell.layer.cornerRadius = 25;
        cell.labellpersons.textAlignment = .Center
    }     

     return cell
}


  func collectionView(collectionView: UICollectionView,  didSelectItemAtIndexPath indexPath: NSIndexPath)
{

   let acelll = collectionVieww.cellForItemAtIndexPath(indexPath)

  acelll?.backgroundColor = UIColor (red: 49/255, green: 30/255,   blue: 49/255, alpha:1.0)
  acelll?.layer.borderWidth = 2.0
    acelll?.layer.borderColor = UIColor .whiteColor() .CGColor


    IndexValue .addObject(indexPath.row)
    print("indexavalueis",IndexValue)

}

internal func collectionView(collectionView: UICollectionView,  didDeselectItemAtIndexPath indexPath: NSIndexPath)
{
  let acell = collectionView .cellForItemAtIndexPath(indexPath)
    acell?.backgroundColor =   self.colorWithHexString(arrColors[indexPath.row])
   acell!.layer.borderWidth = 1.0;
    acell!.layer.borderColor = UIColor .blackColor() .CGColor


   IndexValue.removeObject(indexPath.row)
        print(IndexValue)

}

0 个答案:

没有答案