Newbie question.
I have a custom UICollectionView cells with UIImage and UIButton in every cell. I need to have array of UIImages that will be filled after cell’s checkbox will be tapped to “checked” state (custom UIButton with appropriate checked/unchecked images connected to IBAction with changing internal cell’s BOOL and checkbox state image). I can’t use -didSelectItemAtIndexPath: for marking because I’m using it for previewing a cell photo. First idea was to add cell image to NSMutableArray by action of tapping checkbox to “checked” state, but if user will want to uncheck checkbox I can’t track what exactly UIImage I need to delete from array. What is most painful method to do this?
PS: it is a listing of my method connected to the checkbox
- (void)theCheckboxTapped:(UIButton*)sender
{
cell = (CustomCell *)sender.superview.superview;
if (cell)
{
if ([cell isSelected] == NO)
{
UIImage *img1 = [UIImage imageNamed:@"checked.png"];
[[(CustomCell *)cell checkboxSign] setImage:img1 forState:UIControlStateNormal];
}
if ([cell isSelected] == YES)
{
UIImage *img2 = [UIImage imageNamed:@"unchecked.png"];
[[(CustomCell *)cell checkboxSign] setImage:img2 forState:UIControlStateNormal];
}
cell.isSelected =! cell.isSelected;
}
}
First idea was to made array of cells and check does BOOl of this cell is equal to YES or NO and add appropriate cell images to array
For solving this there are many solutions. But the one which I am explaining, is easy but this consumes some little bit more memory than other methods.
Steps:-
Create two array with capacity cell count. add all images in this array and add only bool value false initially when no cell is select. For this you can add 0.
NSMutableArray *imageArray = [[NSMutableArray alloc]init];
NSMutableArray *boolArray = [[NSMutableArray alloc]init];
When you checked any cell button then replace 0 with 1 in boolArray array, and vice versa.
for changing value you can replaceObjectAtIndex method of nsmutablearray.
Edit:
No need to do like above.
one simpler solution is according to your code here.
As I am seeing that you are able to get cell.
So you can set tag for imageView and depending to tag you can also get cell’s imageview in which you want to add image.
For this create imageArray and all images in this. Now add image like: