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;
}
Related
I am trying to change the color of a button when pressed that is in a tableviewCell. However my code changes the color of every button in the table and not just the one in the cell I selected,
How would I go about just changing the color of the button I pressed.
Please see my code below,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIButton *addNotesButton = (UIButton *)[cell viewWithTag:106];
[addNotesButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Test";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UIButton *addNotesButton = (UIButton *)[cell viewWithTag:106];/
[addNotesButton addTarget:self action:#selector(addNotes :) forControlEvents:UIControlEventTouchUpInside];
}
The main issue might be in your cellForRowAtIndexPath: method. UITableView cells are re-used as they are displayed on the screen. dequeueReusableCellWithIdentifier: method returns a cell if it has been marked as ready for reuse. You must have seen this method of UITableView being used in cellForRowAtIndexPath: method. (See this link)
So in cellForRowAtIndexPath: you will have to configure each cell as it is being loaded or else it will display old values (since the cells are being reused).
You can either declare a property or a simple variable of type NSIndexPath.Let the variable be called _selectedIndexPath. Then in didSelectRowAtIndexPath: you can assign this property to the indexPath selected.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSArray *indexPaths = nil;
if (_selectedIndexPath) {
indexPaths = #[_selectedIndexPath, indexPath];
} else {
indexPaths = #[indexPath];
}
_selectedIndexPath = indexPath;
[tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
}
- (void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Your Cell Identifier"];
UIButton *addNotesButton = (UIButton *)[cell viewWithTag:106];
if (indexPath.row == _selectedIndexPath.row) {
[addNotesButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
} else {
[addNotesButton setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
}
}
You don't need to change color manually in did select row at index path. Just set the color for UIControlStateSelected and on the action of button tap set the buttons selected property to YES. From your code i think this should work.
inside cell for row at index path method
[addNotesButton setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
and in button action method
-(IBAction)addNotes:(id)sender
{
UIButton *button = (UIButton*)sender;
buttons.selected = !button.isSelected;
}
I think this will work.
After trying everything and failed. I ended up having a hidden value in each row that would change when the button is pressed. So the code reads the value then configures the button for each row.
I am having a radio button in table view cell. This is my radio button
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"myCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIButton *newRadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
newRadioButton.frame = CGRectMake(30, 0, 15, 14.5);
[newRadioButton setImage:[UIImage imageNamed:#"unselect"] forState:UIControlStateNormal];
[newRadioButton setImage:[UIImage imageNamed:#"select"] forState:UIControlStateSelected];
cell.accessoryView = newRadioButton;
if ([indexPath isEqual:selectedIndex])
{
newRadioButton.selected = YES;
}
else
{
newRadioButton.selected = NO;
}
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedIndex = indexPath;
[table reloadData];
}
-(void)radiobtn:(id)sender
{
if([sender isSelected])
{
[sender setSelected:NO];
} else
{
[sender setSelected:YES];
}
}
Its working,But i want to select only one radio button at a time like this image (left). But for me selecting all the radio button (right).
I need to make it look like the first image.
I think you should try:
in YourViewController.h
#interface YourViewController : UITableViewController {
NSIndexPath *selectedIndex
}
in YourViewController.m
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"myCell"];
UIButton *newRadioButton;
newRadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
newRadioButton.frame = CGRectMake(30, 0, 15, 14.5);
[newRadioButton setImage:[UIImage imageNamed:#"unselect"] forState:UIControlStateNormal];
[newRadioButton setImage:[UIImage imageNamed:#"select"] forState:UIControlStateSelected];
cell.accessoryView = newRadioButton;
}
if ([indexPath isEqual:selectedIndex]) {
newRadioButton.seleted = YES;
} else {
newRadioButton.seleted = NO;
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
and
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedIndex = indexPath;
[tableView reloadData];
}
this link may help you
Unselect all the radio buttons before your if statement using something like this:
for (UITableViewCell *cell in [self visibleCells]) {
[[cell accessoryView] setSelected:NO];
}
When you are creating the button in cellForRowAtindexpth, attach an unique feature by which you can identify which button is being tapped. To do this UIButton has tag property
you need to set the tag for everybutton inside cellForRowAtindexpth, something like
// where i is just a static int variable for holding the count
myRadioButton.tag = i ;
i=i+1
now you when you tap based on tag, you can check which button is pressed and you can select that one
to know which button is pressed u can know like this
-(IBAction)buttonPressed:(id)sender{
UIButton *button = (UIButton *)sender;
NSLog(#"%d", [button tag]);
}
Try to reuse button
Solution: we can add & access controls by tag from view
if control found by tag we can use it.
if control not found we have to add it with tag.
UIButton *newRadioButton = (UIButton *)cell.accessoryView;
if (!newRadioButton || ![newRadioButton isKindOfClass:[UIButton class]]) {
UIButton *newRadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
newRadioButton.frame = CGRectMake(30, 0, 15, 14.5);
[newRadioButton setImage:[UIImage imageNamed:#"unselect"] forState:UIControlStateNormal];
[newRadioButton setImage:[UIImage imageNamed:#"select"] forState:UIControlStateSelected];
cell.accessoryView = newRadioButton;
}
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];
}
}
}
Am display list of names in UITableview and I'm trying to select any one of the names by using radio button. The problem I face is I can't select the specific row of the cell, either all the rows are selected or deselected.
Code for displaying radio button in cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIButton *report_but;
int flag;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == Nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [user_name objectAtIndex:indexPath.row];
report_but = [UIButton buttonWithType:UIButtonTypeRoundedRect];
report_but.frame = CGRectMake(260,18,20,20);
[report_but setTitle:#"" forState:UIControlStateNormal];
if (flag == 0) {
[report_but setBackgroundImage:[UIImage imageNamed:#"radio_blank.png"] forState:UIControlStateNormal];
}
else
[report_but setBackgroundImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateNormal];
[report_but addTarget:self action:#selector(radio_button:)forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:report_but];
return cell;
}
- (void)radio_button:(id)button
{
NSLog(#"radio button clicked");
NSLog(#"button tag %#",button);
if (flag == 0) {
flag = 1;
}
else
flag = 0;
[table_list reloadData];
}
While row 1 is selected
And I can't use didselectrowatindexpath method for this single row selection, since selecting the row should not select the radio button.
Can any one help me to select the only one row of this tableviewcell.
Try this,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIButton *report_but;
if (cell == Nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIButton *report_but = [UIButton buttonWithType:UIButtonTypeRoundedRect];
report_but.frame = CGRectMake(260,18,20,20);
[report_but setTitle:#"" forState:UIControlStateNormal];
report_but.tag = 8888;
[cell.contentView addSubview:report_but];
}
report_but = (UIButton *)[cell.contentView viewWithTag:8888];
if (selectedIndexPath && selectedIndexPath.row == indexPath.row) {
} else {
}
}
and in radio_button:
- (void)radio_button:(id)button
{
CGPoint pointInTable = [button convertPoint:button.bounds.origin toView:self.tableView];
selectedIndexPath = [self.tableView indexPathForRowAtPoint:pointInTable];
[table_list reloadData];
}
I have created the below code to display buttons in UITableView. I want to add click event for every particular cell in table to reach to google links by clicking into that button.
How to add to clicking events into the buttons options for particular cell and to reach google link?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UIButton *button =[[UIButton alloc]initWithFrame:CGRectMake(5,5,40,40)];
[button addTarget:self action:#selector(buttonpressed:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:#"lori"] forState:UIControlStateNormal];
[button setTag:9001];
[cell addSubview:button];
[cell setIndentationWidth:45];
[cell setIndentationLevel:1];
cell.textLabel.text = [thearray objectAtIndex:indexPath.row];
return cell;
}
-(void) buttonpressed:(UIButton *)sender {}
Try something like:
-(void) buttonpressed:(UIButton *)sender {
NSString* launchUrl = #"http://apple.com/";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: launchUrl]];
}
this will open safari pointing to the URL you pass in.
If you do not want to open safari, then you should display your own UIWebView.
Furthermore, you might want to add your button to contentView:
[cell.contentView addSubview:button];