M Zubair Shamshad
M Zubair Shamshad

Reputation: 2741

how to use uibutton action in uicollectionview cell and get contents of cell

after a lot of search not able to figure out my problem. i am new to iOS development,, so please don't mind if i am wrong…. :P :)

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

i am doing 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 method

[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 

on taping the button of desired cell button BG image is changed. and there is also some other random cell's button Background image is changed…

what is the problem here and second thing is

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

Upvotes: 4

Views: 4635

Answers (1)

Wain
Wain

Reputation: 119031

Your problems are reuse and data management.

First, you are reusing cells by dequeuing them from a pool. You then add a new button with default configuration. So, each time the cell is reused you will add a new button (even if one is already there) and you won't give it any special configuration (part of your data management issue).

For data management, just changing the status of a button isn't enough - you also need to update your data model with the new status represented by the button selection. Then, next time you display that status you can set it to the correct value.

The general approach you should take is to subclass UICollectionViewCell so that you can add your button (only once). The cell subclass should be the target of the button (not the controller). The cell subclass could also have an @property which is the part of the data model associated with it so that it can update the contents when the button is tapped. And, when the property is set the cell can update itself based on the contents (set the appropriate button configuration). Alternatively, the cell could callback to the controller with the appropriate information (and the controller could configure the button in the data source methods).

Upvotes: 4

Related Questions