When I scroll the UITableViewCell ,before selected images changing in iOS - ios

I need to implement this functionality.please help me.
After "unblockbtnClick" click change the image ,it's fine ,but when scroll the tableview the selected images all are changing to previous images.
Here is my code.
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"BlockedCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
{
NSArray *nib;
nib = [[NSBundle mainBundle] loadNibNamed:#"BlockedCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
[btn_tblvcAddbtn addTarget:self action:#selector(unblockbtnClick:) forControlEvents:UIControlEventTouchUpInside];
[btn_tblvcAddbtn setTag:indexPath.row];
return cell;
}
-(IBAction)unblockbtnClick:(UIButton *)sender{
NSLog(#"Value of selected button = %ld",(long)[sender tag]);
[sender setBackgroundImage:[UIImage imageNamed:#"remove.png"] forState:UIControlStateNormal];
}

You need to assign the nib file outside if condition
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
{
// initialize cell here
}
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"BlockedCell" owner:self options:nil];
cell = [nib objectAtIndex:0];

-(IBAction)unblockbtnClick:(id)sender
{
UIButton *btn=(UIButton *)sender;
UITableViewCell *cellll;
if([[[UIDevice currentDevice] systemVersion] intValue]<6)
cellll=(UITableViewCell *)[btn superview];
else
cellll=(UITableViewCell *)[[btn superview]superview];
NSIndexPath *aaa=[tblOption indexPathForCell:cellll];
if([ArrSelectedData containsObject:[TblDataArrr objectAtIndex:aaa.row]])
{
//tblDataArr is table dataArray && ArrSelectedData is a array which contain selected itme of table
[ArrSelectedData removeObject:[TblDataArrr objectAtIndex:aaa.row]];
[btn setBackgroundImage:[UIImage imageNamed:#"blank_checkbox.png"] forState:UIControlStateNormal];
}
else
{
[ArrSelectedData addObject:[TblDataArrr objectAtIndex:aaa.row]];
[btn setBackgroundImage:[UIImage imageNamed:#"tick_checkbox.png"] forState:UIControlStateNormal];
}
}
and this in cellForRowAtIndexPath
{
if([ArrSelectedData containsObject:[TblDataArrr objectAtIndex:indexPath.row]])
[tempBtn setBackgroundImage:[UIImage imageNamed:#"tick_checkbox.png"] forState:UIControlStateNormal];
else
[tempBtn setBackgroundImage:[UIImage imageNamed:#"blank_checkbox.png"] forState:UIControlStateNormal];
}

When you're scrolling a cell out of the screen and then back - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method is called. You're not setting the background there, this is why it gets reset.
To fix you need to store information about selected cell and check it in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath and apply proper background accordingly.

Related

How to Handle Favourite button clicks in custom Tableview cells iOS?

I am developing one app and i have one requirement that. I have to handle the Favourite button on custom cells like. i am creating the custom button with image on cell and setting unselected type Favourite image i am giving by default once user click on the Favourite button on cell i am changing the button image like selected favourite. I am using the following code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #“CustomCell”;
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.favButton.tag = indexPath.section;
[cell.favButton addTarget:self action:#selector(handleFavouriteButton:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
Button Action:
-(void)handleFavouriteButton:(id)sender
{
UIButton *button = sender;
NSLog(#"selected favourite button tag %li", (long)button.tag);
if (button.selected) {
[button setBackgroundImage:[UIImage imageNamed:#"favourites-normal.png"] forState:UIControlStateNormal];
}
else{
[button setBackgroundImage:[UIImage imageNamed:#"favourites-Selected.png"] forState:UIControlStateNormal];
}
button.selected=!button.selected;
}
Using the above code i am able to change the change the Favourite button from normal to selected and selected to normal but problem is when i select the favourite button on first row it is changing 6 and 11 Ect.. rows also.
Can anybody suggest me right way to do this
Thanks in advance.
That button action and all things are looks fine. You need to save selected Button index as a tag in to the NSMutableArray like following example:
In.h Class:
interface myclass : UIViewController{
}
#property (strong, nonatomic) NSMutableArray *arrcontainstate;
In.m Class:
- (void)viewDidLoad {
[super viewDidLoad];
_arrcontainstate=[NSMutableArray array];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #“CustomCell”;
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.favButton.tag = indexPath.row;
if ( [_arrcontainstate containsObject:indexPath.row]) {
[cell.favButton setBackgroundImage:[UIImage imageNamed:#"favourites-Selected.png"] forState:UIControlStateNormal];
}
else {
[cell.favButton setBackgroundImage:[UIImage imageNamed:#"favourites-normal.png"] forState:UIControlStateNormal];
}
[cell.favButton addTarget:self action:#selector(handleFavouriteButton:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
-(void)handleFavouriteButton:(id)sender
{
UIButton *button = sender;
button.selected=!button.selected;
NSLog(#"selected favourite button tag %li", (long)button.tag);
if (button.selected) {
[_arrcontainstate addObject:button.tag];
[button setBackgroundImage:[UIImage imageNamed:#"favourites-Selected.png"] forState:UIControlStateNormal];
}
else
{
[_arrcontainstate removeObject:button.tag];
[button setBackgroundImage:[UIImage imageNamed:#"favourites-normal.png"] forState:UIControlStateNormal];
}
}
As you are reusing the cell, the image of the button is not changing. First cell is reused in 6th and 11th cell so the button image remains selected. Use this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #“CustomCell”;
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.favButton.tag = indexPath.section;
[cell.favButton setBackgroundImage:[UIImage imageNamed:#"favourites-normal.png"] forState:UIControlStateNormal];
[cell.favButton addTarget:self action:#selector(handleFavouriteButton:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}

UITableViewCell selected buttons change to unselected state upon scroll the UITableView

The button action is SongsSelectionSongs_Click. When I click this button, the button image changing, the button tap count is getting correct and after selected button images also changing, but when I scroll back and forth in the UITableView the button image seems to be randomly changing.
This is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"SongsTAbleViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SongsTAbleViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
btn_songSelect.tag = indexPath.row;
lbl_songLabel.text = [[arr_tablVArray objectAtIndex:indexPath.row] objectForKey:#"SongTitle"];
lbl_artistLabel.text = [[arr_tablVArray objectAtIndex:indexPath.row] objectForKey:#"SongArtist"];
return cell;
}
-(IBAction)SongsSelectionSongs_Click:(id)sender
{
UIButton *button = sender;
CGPoint correctedPoint = [button convertPoint:button.bounds.origin toView:self.tblv_SongslisttableView];
NSIndexPath *indexPath = [self.tblv_SongslisttableView indexPathForRowAtPoint:correctedPoint];
NSLog(#"Button tapped in row %d",indexPath.row);
SelectedAlbumUrl = [[arr_tablVArray objectAtIndex:indexPath.row] objectForKey:#"SongUrl"];
str_songtitle = [[arr_tablVArray objectAtIndex:indexPath.row] objectForKey:#"SongTitle"];
if ([[button backgroundImageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:#"add.png"]])
{
btn_songsShareButton.userInteractionEnabled = YES;
[btn_songSelect setBackgroundImage:[UIImage imageNamed:#"remove.png"] forState:UIControlStateNormal];
buttonStatus = buttonStatus +1;
[btn_songsShareButton setImage:[UIImage imageNamed:#"share selected.png"] forState:UIControlStateNormal];
}
else
{
[btn_songSelect setBackgroundImage:[UIImage imageNamed:#"add.png"] forState:UIControlStateNormal];
buttonStatus = 1;
[btn_songsShareButton setImage:[UIImage imageNamed:#"share unselected.png"] forState:UIControlStateNormal];
}
}
You are not doing anything within your cellForRowAtIndexPath to select or deselect image. When you reuse a cell, it doesn't change the state of the cell unless you explicitly tell it to in cellForRow. Therefore, it will either reuse a selected or deselected cell (whatever is the first available reusable cell) and put that on the screen as-is.
To fix this issue, you need logic in your cellForRowAtIndexPath method to either select or deselect the image based on what is appropriate.
In general, if your problem has anything to do with "my cells don't show up right when scrolling" odds are you're not reusing your cells properly.
EDIT: in response to your comment, no, I will not rewrite your code. But I will give you some direction.
I would recommend keeping an additional key/value on your arr_tablVArray that will track whether or not the "share" should be enabled or disabled (I would suggest a bool value). This would make it so that you could check whether or not the "share" is enabled/disabled by checking a bool instead of checking the contents of the button's image in your IBAction method.
This info would now be available in your cellForRowAtIndexPath method as well, and you could check the value for the current record in arr_tablVArray and set your images accordingly just like you set your lbl_songLabel and lbl_artistLabel.
//Try It, it's Working Fine
pragma .h File
NSMutableArray * rowIdArray;
pragma .M File
#synthesize rowIdArray;
- (void)viewDidLoad
{
rowIdArray=[[NSMutableArray alloc]init];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [NamesArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
ViewControllerCell *cell = (ViewControllerCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
NSArray *nib;
nib = [[NSBundle mainBundle] loadNibNamed:#"ViewControllerCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
// Configure the cell...
cell.nameslbl.text = [NamesArray objectAtIndex:indexPath.row];
cell.nameBtn.tag=indexPath.row;
[cell.nameBtn addTarget:self action:#selector(NamesClick_Tapped:) forControlEvents:UIControlEventTouchUpInside];
NSString *a=[NSString stringWithFormat:#"%d", indexPath.row];
NSString *b=[[NSString alloc]init];
for (int i=0;i<[rowIdArray count];i++)
{
b=[rowIdArray objectAtIndex:i];
if ([a isEqualToString:b])
{
UIImage *buttonImage = [UIImage imageNamed:#"star_selected.png"];
[cell.nameBtn setBackgroundImage:buttonImage forState:UIControlStateNormal];
}
}
return cell;
}
-(IBAction)NamesClick_Tapped:(id)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.NameTableView];
NSIndexPath *indexPath = [self.NameTableView indexPathForRowAtPoint:buttonPosition];
NSString *rowIdStr = [NSString stringWithFormat:#"%d", indexPath.row];
if(![rowIdArray containsObject:rowIdStr])
{
[rowIdArray addObject:rowIdStr];
}else{
[rowIdArray removeObject: rowIdStr];
}
[self.NameTableView reloadData];
}
when you reuse a cell where the button has been already set, the same button appears with the previously set image. Instead of creating a new button every time you need a cell, you should just be resetting the state of an existing button. This link might help you.

UIButton color changes after scrolling in UITableView

I have a tableview and I am setting its cells in xib. I have a button in it and when I click it, it changes button color from red to green. When I scroll tableview and come back to green button, it returns to red again. Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"ProgramCell";
ProgramCell *cell = (ProgramCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ProgramCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
[cell.btnHatirlat addTarget:self action:#selector(remind:)
forControlEvents:UIControlEventTouchUpInside];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.contentView setUserInteractionEnabled: NO];
return cell;
}
- (void) remind:(UIButton *)sender {
UIButton *btnTemp = (UIButton *)sender;
int iTag = btnTemp.tag;
// I want to change image of sender
[btnTemp setBackgroundColor:[UIColor greenColor]];
}
EDIT: I have added my solution as an answer. I hope it will help.
I'd do this:
The Tableview doesn't know how to "remember" the button state when it reuses the cell.
I'd create a NSMutableArray which holds a BOOL (or any other) flag wether the button is "on" or "off". Init that Array with all "off" if you like and then change the value at the index corresponding with the indexPath.row accordingly on click to "on".
Read the value in cellForRowAtIndexPath and change the color accordingly.
If you got multiple sections, create an Array for each section and let it hold an Array for the respective number of cells.
If that tableView is backed up by a DB add a flag for the state (on/off), and save it. Out of my head I don't know wether this requires a reloadData for everything to work as expected.
You need store pressed button's cell index.
Try this->
- (void)remind:(UIButton *)sender {
UIButton *btnTemp = (UIButton *)sender;
self.iTag = btnTemp.tag;
/*For many buttons
[btnTemp setSelected:![btnTemp isSelected]];
self.buttonStates[btnTemp.tag] = #([btnTemp isSelected);
*/
// I want to change image of sender
[btnTemp setBackgroundColor:[UIColor greenColor]];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"ProgramCell";
ProgramCell *cell = (ProgramCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ProgramCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (self.iTag == indexPath.row) {
[cell.btnHatirlat setBackgroundColor:[UIColor greenColor]];
}
/*For many buttons
[cell.btnHatirlat setSelected:[self.buttonStates[indexPath.row] boolValue]];
if ([cell.btnHatirlat isSelected]) {
[cell.btnHatirlat setBackgroundColor:[UIColor greenColor]];
}
*/
[cell.btnHatirlat addTarget:self action:#selector(remind:)
forControlEvents:UIControlEventTouchUpInside];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.contentView setUserInteractionEnabled: NO];
return cell;
}
Good luck!
Here is my solution and I want to thank again to #Bernard Grabowski and #MobiDevCom. I have saved clicked button's section and row number and checked if it is clicked or not.
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"ProgramCell";
ProgramCell *cell = (ProgramCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ProgramCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSString* strTemp = [NSString stringWithFormat: #"%d/%d", indexPath.section, indexPath.row ];
//To check clicked button at current section and row is clicked
for (int i = 0; i < buttonClickedArray.count; i++)
{
if([[self.buttonClickedArray objectAtIndex:i] isEqualToString:strTemp])
[cell.btnHatirlat setBackgroundColor:[UIColor greenColor]];
}
NSString *str = [[etkinlikArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
[cell.btnHatirlat addTarget:self action:#selector(remind:)
forControlEvents:UIControlEventTouchUpInside ];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.contentView setUserInteractionEnabled: NO];
}
return cell;
}
- (void) remind:(UIButton *)sender
{
//to get clicked button's indexpath
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonPath = [self.tableProgram indexPathForCell:clickedCell];
self.currentIndexPathStr = [NSString stringWithFormat: #"%d/%d", clickedButtonPath.section, clickedButtonPath.row];
[self.buttonClickedArray addObject:currentIndexPathStr];
[btnTemp setBackgroundColor:[UIColor greenColor]];
}

UITableViewCell add button

I want to add some buttons to custom cell, when I click the button and other buttons also change. How can I click a button without changing the other buttons?
enter code here
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"cell";
SViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[SViewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier];
}
cell.cellButton.tag = indexPath.row;
[cell.cellButton addTarget:self action:#selector(clickMe:)
forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)clickMe:(UIButton *)sender
{
SViewTableViewCell *cell = (SViewTableViewCell *)[[sender superview]superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
if (sender.tag == 0) {
if ([sender.titleLabel.text isEqualToString:openStr]) {
[sender setTitle:closeStr forState:UIControlStateNormal];
}
else{
[sender setTitle:openStr forState:UIControlStateNormal];
}
}
}
I think it's a issue about tableView cell reuse again, you should set the button title in cellForRow to keep reuse cell have a correct title, you should try code like this:
openDict is a NSMutableArray you should define, and this just a example.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"cell";
SViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[SViewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier];
[cell.cellButton addTarget:self action:#selector(clickMe:)
forControlEvents:UIControlEventTouchUpInside];
}
NSString *key = [NSString stringWithFormat:#"%d-%d", indexPath.row, indexPath.section];
if (openDict[key] && [openDict[key] boolValue]) {
[sender setTitle:openStr forState:UIControlStateNormal];
}
else{
[sender setTitle:closeStr forState:UIControlStateNormal];
}
cell.cellButton.tag = indexPath.row;
return cell;
}
- (void)clickMe:(UIButton *)sender
{
SViewTableViewCell *cell = (SViewTableViewCell *)[[sender superview]superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
if (sender.tag == 0) {
NSString *key = [NSString stringWithFormat:#"%d-%d", indexPath.row, indexPath.section];
if (openDict[key] && [openDict[key] boolValue]) {
openDict[key] = #(0);
[sender setTitle:closeStr forState:UIControlStateNormal];
}
else{
openDict[key] = #(1);
[sender setTitle:openStr forState:UIControlStateNormal];
}
}
}
Please set tag for each button in custom tableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"cell";
SViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[SViewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier];
}
UIButton *button1 = (UIButton *)[cell viewWithTag:1];
UIButton *button2 = (UIButton *)[cell viewWithTag:2];
[button1 addTarget:self action:#selector(clickMe:)
forControlEvents:UIControlEventTouchUpInside];
[button2 addTarget:self action:#selector(clickMe:)
forControlEvents:UIControlEventTouchUpInside];
return cell;
}
To add a button to a UITableViewCell you should do it at the time you configure the cell in cellForRowAtIndexPath - this is after you get the cell to reuse. If you want to add a button to a small number of cells you should cause that cell to be reloaded using UITableView's – reloadRowsAtIndexPaths:withRowAnimation: method or reloadData to give you the opportunity to reload all cells.
Any subview you add to a cell should be added to the cell's contentView as a subview. You should remove all such views in prepareForReuse or you could find cell reuse makes extra buttons appear in cells you don't want to have buttons. Because of the need to prepare cells for reuse it is probably best to subclass UITableViewCell and to provide methods on the subclass to add buttons.
Another way is to register multiple kinds of cells for reuse with the table view, choosing what kind of cell to retrieve and use based on the data backing your table view. This is as simple as using registerClass:forCellReuseIdentifier: once for each kind of cell, then applying right reuse identifier in dequeueReusableCellWithIdentifier:forIndexPath: - you'll still be subclassing the cell objects but you configure them further in the class rather than at the time they are dequeued.
Use the code below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"cell";
SViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[SViewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.cellButton.tag = indexPath.row;
[cell.cellButton addTarget:self action:#selector(clickMe:)
forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)clickMe:(UIButton *)sender {
SViewTableViewCell *cell = (SViewTableViewCell *)[[[sender superview]superview] superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
[self keepSelectionRemainsSameForIndexPath:indexPath andSender:sender];
}
- (void)keepSelectionRemainsSameForIndexPath:(NSIndexPath *)indexPath andSender:(UIButton *)sender {
if (sender.tag == indexPath.row) {
if ([sender.titleLabel.text isEqualToString: openStr]) {
[sender setTitle:closeStr forState:UIControlStateNormal];
} else {
[sender setTitle: openStr forState:UIControlStateNormal];
}
}
}

Tableview images chaging when scrolling using custom tableview

I have a Tableview and i am using the customTableview cell for displaying the cell
in the cell i have 5 buttons (rating buttons) when i click on particular button the image of the button has to changed its working fine but when i am scrolling the tableview again thay are changing to normal rating buttons see the following images for clarity
This is the image before scrolling and clicking nothing on button
after clicking the on rating buttons the image is changing like this
but when scrolling the cells again its changing to first image
please help me out
Code :
- (UITableViewCell )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
dataCell = (DataCel)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (dataCell==nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"DataCel" owner:self options:nil]; dataCell = [nib objectAtIndex:0];
}
return dataCell;
}
you should save the sate of the button images
and in your
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
insert code to keep them updated
for ex:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//create your cell here if it was not created
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
[cell.textLabel setFont:[UIFont boldSystemFontOfSize:13]];
[cell.textLabel setTextColor:[UIColor darkGrayColor]];
[cell.detailTextLabel setFont:[UIFont boldSystemFontOfSize:11]];
[cell.detailTextLabel setTextColor:[UIColor lightGrayColor]];
cell.detailTextLabel.numberOfLines = 2;
}
NSArray *array = [imageArray objectAtIndex:[indexPath row]];
if([[array objectAtIndex:3] isEqual:#"rate3"])
{
//set the code for the rating 3
}
else
{
//insert another rate depending on your object
}
This is because every time you scroll the UITableView, it refreshes its values from the data you provided it ! So the value is not retained. To prevent this from happening every time you scroll the database you can use this
static NSString *cellIdentifier = #"Identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
}
for(id obj in cell.contentView.subviews)
{
if([obj isKindOfClass:[UIImageView class]] || [obj isKindOfClass:[UILabel class]])
{
[obj removeFromSuperview];
}
}
This would not let the values be overlapped.
When scrolled the cells are recreated and so you need the keep the selected values in a seperate array and set it in the cellForRowAtIndexPath method
This happens because every time you scroll the tableview , it re-create tableview cell. therefor you can use array for reused it.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = #"cellId";
UITableViewCell *cell = [categoriesTableview dequeueReusableCellWithIdentifier:cellId];
if (cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
for (UIView *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}
// ui goes here
return cell
}

Resources