Strange behavior when adding UIButton to UICollectionViewCell - ios

I'm trying to add aUIButton to aUICollectionViewCell. The main purpose of the button is to let users to select their favourites.
When they click on theUIButton, the UIButton will display a glowing star image, showing that the item has been picked as favourite.
Click on the UIButton again, and the button return to display the original image with an empty star, meaning deselection.
The way I realize this is as below:
1 - In the storyboard, put an UIButton into theUICollectionViewCell, ctrl-drag the button to the cell's .h file to setup an action called
-(IBAction)favoriteBtnClick:(id)sender;
2 - In the cell's .m file, edit the action as follows:
-(IBAction)favoriteBtnClick:(id)sender {
if ([self.favoriteChecked isEqualToString:#"NO"]) {
UIImage *image = [UIImage imageNamed:#"FavoriteSelected.PNG"];
[sender setBackgroundImage:image forState:UIControlStateNormal];
self.favoriteChecked = #"YES";
} else if ([self.favoriteChecked isEqualToString:#"YES"]) {
UIImage *image = [UIImage imageNamed:#"FavoriteBlank.PNG"];
[sender setBackgroundImage:image forState:UIControlStateNormal];
}
}
However, when I try to run the UICollectionView, when I clicked the button on one cell, "strange" behaviors occurred:
1 - Click on one cell to turn one button to favorite glowing star, buttons on some other cells below also turn selected, after your scroll down the list.
2 - The switching only functions for one cycle. After you finish the first round of select and deselect, there is no longer reactions upon further clicking.
Can anybody help me understand the reasons for this and probably give me some hints on how to solve it?
Thank you!
Regards,
Paul

Add this code in your button action
CGPoint ButtonPoint = [sender convertPoint:CGPointZero toView:self.collectionView];
NSIndexPath *ButtonIndex = [self.collectionView indexPathForItemAtPoint:ButtonPoint];
buttonindexpath = ButtonIndex.row
[collectionView reloadData];
Now in your cellForItemAtIndexPath, check if indexPath.row == buttonindexpath, if yes give the code you gave in button action....
ie....
if (indexPath.row == buttonindexpath)
{
if ([self.favoriteChecked isEqualToString:#"NO"])
{
UIImage *image = [UIImage imageNamed:#"FavoriteSelected.PNG"];
[sender setBackgroundImage:image forState:UIControlStateNormal];
self.favoriteChecked = #"YES";
}
else if ([self.favoriteChecked isEqualToString:#"YES"])
{
UIImage *image = [UIImage imageNamed:#"FavoriteBlank.PNG"];
[sender setBackgroundImage:image forState:UIControlStateNormal];
}
}
Hope this works fine

This is happening because of cell reuse. since self.favoriteChecked is a property inside the custom collection cell class, and its value is updated only inside that class causes this issue.
Solution to this problem is, if you are using custom object model to populate collection cells from viewcontroller, then on favourite button click update the favourite status on that custom object and in cellForItemAtIndexPath method check the value of favourite status and update the favoriteChecked status of custom collectionviewcell class.
If you are not using custom object, you need to keep a list of favourite items separately either indexpaths of favourite item or unique identifier of fav item. Then in cellForItemAtIndexPath method check the value of favourite status and update the favoriteChecked status of custom collectionviewcell class.

This is no no a strange behaviour. You have load the cells from the model data objects. You have to track & keep the state change of button clicked data in an array & then load the cell data values.
Here is a sample code : which aligns with solution to your problem.
NSMutableArray *favoriteStatusArray = [[NSMutableArray alloc]init];
// assume favoriteStatusArray is initialized with all BOOL as NO.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
SomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
cell.favoriteButton.tag = indexPath.row;
[cell.favoriteButton addTarget:self action:#selector(peformFavoriteChange:) forControlEvents:UIControlEventTouchUpInside];
if([[favoriteStatusArray objectAtIndex:indexPath.row] boolValue] == NO)
UIImage *image = [UIImage imageNamed:#"FavoriteSelected.PNG"];
[cell.favoriteButton setBackgroundImage:image forState:UIControlStateNormal];
}else {
UIImage *image = [UIImage imageNamed:#"FavoriteBlank.PNG"];
[cell.favoriteButton setBackgroundImage:image forState:UIControlStateNormal];
}
return cell;
}
-(void)peformFavoriteChange:(UIButton *)sender
{
BOOL oldValue = [[favoriteStatusArray objectAtIndex:sender.tag] boolValue];
[favoriteStatusArray replaceObjectAtIndex:sender.tag withObject:[NSNumber numberWithBool:!oldValue]];
[myCollectionView reloadData];
}
Hope you got the idea of doing this.

Related

Messing UIButton in each UICollectionViewCell in Scrolling Filmstrip

This is a follow-up question to my previous one and this time I have a problem with UIButton that I have added in each UICollectionViewCell. Before diving into the problem, let me brief out all I've got so far.
Here's the basic rundown of how my UICollectionView in Scrolling Filmstrip style within UITableView works:
Create a normal UITableView with a custom UITableViewCell
Create a custom UIView that will be added to the cell's contentView
The custom UIView will contain a UICollectionView
The custom UIView will be the datasource and delegate for the
UICollectionView and manage the flow layout of the UICollectionView
Use a custom UICollectionViewCell to handle the collection view data
Use NSNotification to notify the master controller's UITableView when
a collection view cell has been selected and load the detail view.
So far, I've been able to add UIButton in each UICollectionViewCell and when I tap the UIButton its image will change to checked mark but the problem occurs when I scroll up/down. Once the cell with checked mark UIButton is off-screen and scrolled back on-screen again, UIButton image starts to get messed around. For example, when UIButton image in cell1 should be checked mark but it's not. Instead checked mark will be appeared in another cell.
Here below is my relevant code:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
PostsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"PostsCollectionViewCell" forIndexPath:indexPath];
NSDictionary *cellData = [self.collectionData objectAtIndex:[indexPath row]];
cell.postTextLabel.text = [cellData objectForKey:#"text"];
cell.locationNameLabel.text = [cellData objectForKey:#"locationName"];
// >>> Select Button <<<
UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
[selectButton setFrame:CGRectMake(110, 150, 20, 20)];
[selectButton setImage:[UIImage imageNamed:#"uncheckedDot.png"] forState:UIControlStateNormal];
[selectButton setImage:[UIImage imageNamed:#"checkedDot.png"] forState:UIControlStateSelected];
[cell.contentView addSubview:selectButton];
[selectButton addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
// >>> End Select Button <<<<
return cell;
}
// >>> Select Button Method <<<
-(void) buttonPressed:(UIButton *)sender
{
if([sender isSelected]){
//...
[sender setSelected:NO];
} else {
//...
[sender setSelected:YES];
}
}
Any help would be greatly appreciated! Thanks in advance.
Man, you should be aware that cells (both for UITableView and UICollectionView) are reused via the reuse deque under the hood of UIKit and are not obligated to keep their state consistent with regard to their position in a table view or a collection view. What you need to do is to make sure that the return value of dequeueReusableCellWithReuseIdentifier: is then properly initialized (or, in your case, re-initialized) in accordance with the state of your data model. Basically, you need to store the state of your "checkmarks" (or "checkboxes", what have you) in an array or any other data structure. Then, when you call dequeueReusableCellWithReuseIdentifier:, you should apply that stored state to the return value (the cell you're going to return for the collection view) you just got. Hope it's explained good enough. Good luck.
Maintain in your Data source which cell is to be selected by adding a key for each data source dict. For example:#"isSelected"
Then in cellForRowAtIndexPath:
if ([[cellData valueForKey:#"isSelected"] boolValue]) {
[selectButton setSelected:NO];
} else {
[selectButton setSelected:YES];
}
and in your -(void) buttonPressed:(UIButton *)sender Method:
CGPoint point = [self.collectionView convertPoint:CGPointZero fromView:sender];
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:point];
NSDictionary *cellData = [self.collectionData objectAtIndex:[indexPath row]];
if ([[cellData valueForKey:#"isSelected"] boolValue]) {
[cellData setValue:#"false" forKey:#"isSelected"];
} else {
[cellData setValue:#"true" forKey:#"isSelected"];
}
[self.framesCollectionView reloadItemsAtIndexPaths:#[indexPath]];
AND
In your cellForRowAtIndexPath set a tag for the UIButton. Then before adding a UIButton as subview to the cell's contentView check if a view with this tag already exists. In case the cell has been reused, just use the already existing button.

Objective C - Problems With TableView Check Box Using Sections

I'm implementing a contact check-box feature in my UITableView by using the following code.
The problem I'm having is that, when checking off a couple of users and then scrolling up/down the table, users who should not be checked off are showing as checked, and vice versa.
I'm assuming it has something to do with an error in my cell creation, but I'm not quite sure what.
- (void)viewDidLoad {
...
self.checkedUsers = [[NSMutableArray alloc] init];
for (int i=0; i<self.allContacts.count; i++) {
[self.checkedUsers insertObject:#"FALSE" atIndex:i];
}
...
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"SocietyContactCell";
SocietyContactCell *cell = (SocietyContactCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SocietyContactCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
BOOL checked = [[self.checkedUsers objectAtIndex:indexPath.row] boolValue];
UIImage *image = (checked) ? [UIImage imageNamed:#"checkedContact.png"] : [UIImage imageNamed:#"uncheckedContact.png"];
[cell.checkButton setBackgroundImage:image forState:UIControlStateNormal];
[cell.checkButton addTarget:self action:#selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonPressedAction:(id)sender
{
UIButton *button = (UIButton *)sender;
int row = button.tag;
BOOL checked = [[self.checkedUsers objectAtIndex:row] boolValue];
[self.checkedUsers removeObjectAtIndex:row];
[self.checkedUsers insertObject:(checked) ? #"FALSE":#"TRUE" atIndex:row];
UIImage *newImage = (checked) ? [UIImage imageNamed:#"uncheckedContact.png"] : [UIImage imageNamed:#"checkedContact.png"];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
}
EDIT: I now realize the issue is likely coming from my use of sections in this TableView. The contacts are sorted into alphabetized sections, which is messing up my tags.
How would I tackle this problem using sections?
The problem here is retention of the status of checkmark in a cell. Since the table cells are reused on scrolling, the cellForRowAtIndexPath method is called again and the value of the check mark is not set again there.
You will have to make a global array which will store the status of the checkmark for each indexPath. Then you can set the status of the checkmark button checked/unchecked in cellForRowAtIndexPath method.
Add the following code snippet along with the code show:
[cell.checkButton addTarget:self action:#selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.checkButton setTag:indexPath.row];
Also, replace these lines:
[self.checkedUsers removeObjectAtIndex:row];
[self.checkedUsers insertObject:(checked) ? #"FALSE":#"TRUE" atIndex:row];
with this:
[self.checkedUsers replaceObjectAtIndex:row withObject:checked ? #"TRUE" : #"FALSE"];
In your SocietyContactCell Class add the following Method:
- (void)prepareForReuse {
[super prepareForReuse];
[self.checkButton setBackgroundImage:nil forState:UIControlStateNormal];
}
I don't see where you are setting button.tag every time in cellForRowAtIndexPath:, so the action method might be updating the wrong row in your self.checkedUsers array, although the action method independently updates the cell's image correctly at the moment. When you scroll up and down however, the display will go back to the state in the self.checkedUsers array.
You probably need a line like:
cell.checkButton.tag = indexPath.row;
in your cellForRowAtIndexPath: method.
Secondly, your action method is getting the state on self.itsToDoChecked, and then updating a self.checkedUsers array, when it appears it should be acting on the same array (simply reversing the state of the given row index). Not sure if that is just an example code typo or something significant.
Edit: It's also possible there is state in the cell subclass causing issues; hard to be sure. You want to implement -prepareForReuse in that class (and always call super) to reset any state to initial values.

How select one item at a time ios sdk

I have a list of Option values displayed in a UITableView.
Now I want user to select one item once. But currently the user can select all the option.
What I want :
Suppose I Have 5 radio boxes : 1 2 3 4 5 at a time user can only select one . If he choses another one then Previous one must get deselected.
What is Happening Now:
Currently all of the boxes are get selected.
I am using this code in my didSelectRowAtIndex method:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIButton *btnRadhio = (UIButton *)[cell viewWithTag:1];
for(int i =0;i<[arrDistanceList count];i++)
{
[btnRadhio setBackgroundImage:[UIImage imageNamed:#"radio_unchecked"] forState:UIControlStateNormal];
}
[btnRadhio setBackgroundImage:[UIImage imageNamed:#"radio_checked"] forState:UIControlStateNormal];
You are changing the UIButton for same index path:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //From Your Code
You have to get new indexPath each time. Try this one
for(int i =0;i<[arrDistanceList count];i++)
{
NSIndexPath *indexPathI=[NSIndexPath indexPathForRow:i inSection:0]; //i supposed you have 0 section
UITableViewCell *cellI=[tableView cellForRowAtIndexPath:indexPathI];
UIButton *btnRadhio = (UIButton *)[cellI viewWithTag:1];
if(i==indexPath.row)
{
[btnRadhio setBackgroundImage:[UIImage imageNamed:#"radio_checked"] forState:UIControlStateNormal];
}
else
{
[btnRadhio setBackgroundImage:[UIImage imageNamed:#"radio_unchecked"] forState:UIControlStateNormal];
}
}
You can check here also
You need to have one int class variable and store selected indexpath.row into it and reload tableview and in cellForRowAtIndexPath check this variable and check row that you have selected.
I will assume arrDistanceList is an array of objects, each of which represents a row of data in your table.
It is good practice that your UI is driven by your model. So let's say each object in your array has information such as 'title', 'background image' etc, consider a simple boolean flag, e.g. 'selected', which cellForRow consults.
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIButton *btnRadhio = (UIButton *)[cell viewWithTag:1];
id object = [arrDistanceList objectAtIndex:indexPath.row];
if (object /* .selected */) {
[btnRadhio setBackgroundImage:[UIImage imageNamed:#"radio_checked"] forState:UIControlStateNormal];
} else {
[btnRadhio setBackgroundImage:[UIImage imageNamed:#"radio_unchecked"] forState:UIControlStateNormal];
}
When making a selection in didSelect, flip the boolean for the selected object and turn off all other selections by searching with NSPredicate.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"selected == YES"];
NSArray *filteredArray = [arrDistanceList filteredArrayUsingPredicate:predicate];
All you will need to do now is update any visible cells on screen. You can do this using tableView.visibleCells array.
It is fine updating the visible cells with a for loop on the main thread, because the number of visible cells at any one time will be relatively small. However, the arrDistanceList array may have a lot of objets within it, so you may eventually considering updating this array on a background thread one day.
CellforRowAtIndexPath:
cell.btnRadhio.tag = (indexpath.row+1)*100;
DisselectRowAtIndexPath:
for(int i =0;i<[arrDistanceList count];i++)
{
UIButton *btnRadhio = (UIButton *)[self.view viewWithTag:(i+1)*100];
if(i==indexpath.row)
{
[btnRadhio setBackgroundImage:[UIImage imageNamed:#"radio_checked"] forState:UIControlStateNormal];
}
else
{
[btnRadhio setBackgroundImage:[UIImage imageNamed:#"radio_unchecked"] forState:UIControlStateNormal];
}
}
Hope this will help you.

Losing UIButton within a custom cell when reloading table

I've custom UITableViewCells where I placed buttons dynamically in code. I have programmatically set actions for such buttons, and when the action is triggered, then I need to reload the table, among other things. Once reload is called, the sender button disappears, and I don't know why... Could somebody give me a hint for the possible reason? If button is not tapped, and I scroll up and down through the table, it remains there.
Thanks!
EDIT
This is a code snippet from cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *listData =[self.tableViewEntries objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell *cell;
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0: {
cell = [self.tableView dequeueReusableCellWithIdentifier:#"firstCell"];
((CustomCell *)cell).cellTextField.tag = firstCellTag;
cell.tag = firstCellTag;
break;
}
case 1: {
cell = [self.tableView dequeueReusableCellWithIdentifier:#"secondCell"];
((CustomCell *)cell).cellTextField.tag = secondCellTag;
cell.tag = secondCellTag;
break;
}
}
return cell;
}
The CustomCell I've defined is loaded from a nib. Those cells have a UITextField for user input and a UIView where I set or remove the buttons dynamically in code according to the user input within the text field. The buttons are created in code, and their actions are set in code as well:
- (void)setButtonForCell:(UITableViewCell *)cell withResult:(BOOL)isValid
{
UIImage* validationButtonNormalImage = nil;
UIImage* validationButtonHighlightImage = nil;
UIButton *validationButton = [UIButton buttonWithType:UIButtonTypeCustom];
[validationButton setFrame:CGRectMake(0, 0, ((CustomCell *)cell).cellValidationView.frame.size.width, ((CustomCell *)cell).cellValidationView.frame.size.height)];
if (isValid) {
validationButtonNormalImage =[[UIImage imageNamed:#"success.png"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
validationButtonHighlightImage =[[UIImage imageNamed:#"success.png"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
[validationButton removeTarget:self
action:#selector(validationButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
}
else {
validationButtonNormalImage =[[UIImage imageNamed:#"error.png"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
validationButtonHighlightImage =[[UIImage imageNamed:#"error.png"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
[validationButton setTag:cell.tag];
[validationButton removeTarget:self
action:#selector(validationButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
[validationButton addTarget:self
action:#selector(validationButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
}
[validationButton setBackgroundImage:validationButtonNormalImage forState:UIControlStateNormal];
[validationButton setBackgroundImage:validationButtonHighlightImage forState:UIControlStateHighlighted];
[((CustomCell *)cell).cellValidationView.subviews makeObjectsPerformSelector: #selector(removeFromSuperview)];
[((CustomCell *)cell).cellValidationView addSubview:validationButton];
}
Then, in validationButtonTapped:, I do some tasks and call to [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:NO];, and there is when I loose the button from the view. However, if I don`t tap the button and I scroll down and up again to get 'cellForRowAtIndexPath:' called again, the button remains here. It is like I only loose it when calling for a reload.
Any help?
Cells are usually reused if you are following the usual cellForRowAtIndexPath: pattern. Do you remove all subviews in prepareForReuse for each cell? If you didn't then you would see unexpected buttons on cells as you scroll up and down. You probably need to add the buttons as required in each cell's configuration after you get the cell for reuse (or in newer code, dequeue a cell) in cellForRowAtIndexPath:.

IOS: Maintaining button state in uitableviewcell

I have an iPhone app problem that's been bugging me for a few days and it really doesn't seem like it should be this difficult so I'm sure I'm missing something obvious. I have researched plenty of forum discussions on "similar" topics but nothing that actually addresses this issue, specifically.
To be clear, if there is some piece of documentation or some other source that I should research, please point me in the right direction.
Here goes...
I have a list of items that I display to the user within a table (uitableview). The cell (uitableviewcell) for each item is custom and contains an image and a Like button (uibutton). As expected, for each item in the the table, the user can click the Like button or ignore it. The like button calls a separate process to update the server. Simple, right?
So, here is the issue:
When the Like button is clicked on a particular cell, the Selected state works fine but the moment you scroll the cell out of view, other random cells in the table show the Like button as Selected even though they were never touched. Because the cells are reused, for obvious performance reasons, I understand why this could happen. What I don't understand is why my approach (see code below) would not override or reset the button's state the way I think it should. For brevity, I am only including the relevant code here (hopefully formatted properly):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCustomCell";
MyCustomViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MyCustomViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSString *myRating = [[self.dataArray objectAtIndex:indexPath.row] valueForKey:#"my_rating"];
// Create the Like button
UIButton *likeButton = [[UIButton alloc] initWithFrame:CGRectMake(260, 68, 40, 40)];
[likeButton setImage:[UIImage imageNamed:#"thumbsUp"] forState:UIControlStateNormal];
[likeButton setImage:[UIImage imageNamed:#"thumbsUpSelected"] forState:UIControlStateSelected];
if (myRating == #"9") {
[likeButton setSelected:YES];
}
[likeButton setTitle:#"9" forState:UIControlStateNormal];
[likeButton setTag:indexPath.row];
[cell.contentView addSubview:likeButton];
[likeButton addTarget:self action:#selector(likeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)likeButtonPressed:(UIButton *)sender {
// Changed the Selected state on the button
UIButton *button = (UIButton *)sender;
button.selected = !button.selected;
// Create a new object with the user's rating and then replace it in the dataArray
NSString *ratingText = sender.titleLabel.text;
NSMutableArray *myMutableArray = [[self.dataArray objectAtIndex:row] mutableCopy];
[myMutableArray setValue:ratingText forKey:#"my_rating"];
[self.dataArray replaceObjectAtIndex:row withObject:myMutableArray];
}
So, I've been through many iterations of this but I can't seem to get the state of the button to show the Selected image for those items that are Liked and keep the normal image for those items that have not been Liked.
Any help or advice would be greatly appreciated.
there is a simple way out of this. you can trick the button to keep being in the latest selected state.
make a mutable array, for the purpose of keeping the selected state of the button
selectedButton = [[NSMutableArray alloc]init];//i've already defined the array at the .h file
for (int i = 0; i<yourTableSize; i++) //yourTableSize = how many rows u got
{
[selectedButton addObject:#"NO"];
}
at the tableviewcell method, declare your button and set it so that it refers to the mutablearray to set it's selected state
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
UIImage *img = [UIImage imageNamed:#"btn_unselected.png"];
UIButton *toggleButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
[toggleButton setImage:img forState:UIControlStateNormal];
img = [UIImage imageNamed:#"btn_selected.png"];
[toggleButton setImage:img forState:UIControlStateSelected];
[toggleButton setTag:indexPath.row+100];//set the tag whichever way you wanted it, i set it this way so that the button will have tags that is corresponding with the table's indexpath.row
[toggleButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:toggleButton];
//and now we set the button's selected state, everytime the table reuse/redraw the cell the button will set it's selected state according to the array
if([[selectedButton objectAtIndex:indexPath.row]isEqualToString:#"NO"])
{
[toggleButton setSelected:NO];
}
else
{
[toggleButton setSelected:YES];
}
return cell;
and finally, create the method which the button triggered when the button is pressed, to change it's selected state
-(void)buttonPressed:(UIButton*)sender
{
int x = sender.tag - 100; //get the table's row
if([sender isSelected]) //if the button is selected, deselect it, and then replace the "YES" in the array with "NO"
{
[selectedButton replaceObjectAtIndex:x withObject:#"NO"];
[sender setSelected:NO];
}
else if (![sender isSelected]) //if the button is unselected, select it, and then replace the "NO" in the array with "YES"
{
[selectedButton replaceObjectAtIndex:x withObject:#"YES"];
[sender setSelected:YES];
}
}
The problem is that every time you create or reuse a cell you're giving it a new like button, so when you reuse a cell where the like button has been activated, you're giving it a deactivated like button but the old, activated like button is still there as well.
Instead of creating a like button every time you need a cell, you should just be setting the state of an existing like button. See the answers to this question for some possible ways of handling that.
This is not valid (At least not if you are trying to compare strings my contents instead of addresses):
if (myRating == #"9")
Try this:
if ([myRating isEqualToString:#"9"])
And +1 to yuji for noticing the multiple button creation.

Resources