How to use uibutton action in uicollectionview cell and get cell contents

after many searches failed to find out my problem. I am new to iOS development, so please do not mind if I am wrong ....: P :)

I have a button in my collection view cell, when the button is pressed, it should change the background image and get the contents of the cell on which the cell button is clicked.

I do it like this

in my .h file

@interface MyCell  : UICollectionViewCell
@property (nonatomic, strong) UIButton *button;
@end

@interface DetailInvoicing : UIViewController

in .m file

@implementation MyCell
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(175, 1, 50, 30);
    self.button.backgroundColor = [UIColor clearColor];
    [self.button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.button setBackgroundImage:[UIImage imageNamed:@"buy.png"]forState:UIControlStateNormal];
    [self.contentView addSubview:self.button];
}
return self;
}

- (void)buttonClicked:(UIButton *)sender
{
NSLog(@"button clicked!");
self.button.backgroundColor = [UIColor lightGrayColor];
[self.button setTitle:@"Sold" forState:UIControlStateNormal];
[self.button setBackgroundImage:[UIImage imageNamed:nil]forState:UIControlStateNormal];
}
@end

in viewdidload mode

[self CollectionLoad];

and the CollectionLoad method is

// Use sqlite query to fetch data and save it in array, then
myCollection.delegate = self;
myCollection.dataSource = self;
[myCollection reloadData];
myCollection.backgroundColor=[UIColor clearColor];
[myCollection registerClass:[MyCell class] forCellWithReuseIdentifier:@"CellID"];

then

use datasource and delegate methods 

when the button of the cell button is pressed, the BG button changes. and there is another random cell button. The background image is changing ...

what is the problem and the second is

how to get the contents of a cell on this button.

+4
1

- .

-, , . . , , , ( ), - ( ).

- , . , , , .

, , UICollectionViewCell, ( ). ( ). @property, , . , , ( ). , ( ).

+4

Source: https://habr.com/ru/post/1534019/


All Articles