How to select radio button at a time in table view - ios

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;
}

Related

checkBox action event in uitableviewcell

I am having custom-cell in which i put the UIButton as the CheckBox.
I want the array of selected checkBox indexPath in ViewController.
I am trying to set the UIButton Action event into the ViewController but it is not accessible. Here is my Code,
-(UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCellSaveSearchTableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:#"SaveSearchCell"];
if(!cell)
{
cell = [[[NSBundle mainBundle]loadNibNamed:#"CustomTableViewCellSaveSearchTableViewCell" owner:self options:nil]objectAtIndex:0];
}
[self setText:cell];
[cell.btnDelete addTarget:self action:#selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
cell.btnDelete.tag = indexPath.row;
return cell;
}
-(void)btnClick : (UIButton *)btn
{
// I Can't accaes this method.
}
How can i get this.
UITableViewCells have a state of their own to reflect if they are selected or not. You don't need a button in the cells (it is discouraged for UX reasons anyway). Check here for a tutorial.
Do according to these three floowing method.
- (void)viewDidLoad {
[super viewDidLoad];
chekMarkArray = [[NSMutableArray alloc]init];
int totalData = 10;
chekMarkArray=[[NSMutableArray alloc] initWithCapacity:totalData];
for (int i=0; i<totalData; i++) {
[chekMarkArray addObject:#(0)];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIButton *testButton;
if (cell == nil ||1) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
testButton.frame = CGRectMake(100, 5,25, 25);
testButton.tag = indexPath.row;
BOOL selectindex = [[chekMarkArray objectAtIndex:indexPath.row] boolValue];
if (!selectindex) {
testButton.backgroundColor = [UIColor greenColor];
}else{
testButton.backgroundColor = [UIColor redColor];
}
[testButton addTarget:self action:#selector(DropDownPressed:) forControlEvents:UIControlEventTouchDown];
[cell addSubview:testButton];
}
return cell;
}
checked button action
-(void)btnClick : (UIButton *)btn
{
UIButton *button = (UIButton*)sender;
BOOL currentStatus = [[chekMarkArray objectAtIndex:button.tag] boolValue];
//deselect all selection
for (int i=0;i<chekMarkArray.count;i++) {
[chekMarkArray replaceObjectAtIndex:i withObject:#(0)];//comment this line for multiple selected button
}
//select current button
[chekMarkArray replaceObjectAtIndex:button.tag withObject:#(!currentStatus)];
[self.tableView reloadData];
}
You may use this code instead of button
first Alloc NsmutableArray in ViewDidLoad
and put an Button outside Tableview to complete your action.
then add
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[selectedRowsArray addObject:[arrShareTickets objectAtIndex:indexPath.row]];
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[selectedRowsArray removeObject:indexPath];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

Adding buttons to a UITableView cell

I am trying to add buttons to my UITableView cells, using code like below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
UIButton *button = self.buttons[0];
[cell addSubview:button];
return cell;
}
The button isn't showing though. Is it possible to add buttons to a cell like this or do I have to make a custom UITableViewCell class?
When the button was made for the array the frame was set like below:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 70)]
The usage for adding elements have changed somewhen. Use
[cell.contentView addSubview:button];
and don't forget to proof before if a button is already added :P
enum
{
kButtonForA = 3333 // example
};
for every button set
button.tag = kButtonForA;
and use them
UIButton* btn = (UIButton *)[cell.contentView viewWithTag:kButtonForA];
if(!button)
btn = self.buttons[i];
UPDATE
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if(!self.buttons || [self.buttons count] == 0)
return 0;
return ...;
}
and after you init your array, use
[tableView reloadData];
just one possible way, but a fast one
try this in your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([cell.contentView viewWithTag:100] == nil) {
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(130,25, 40.0, 40.0);
[button setTitle:#"+" forState:UIControlStateNormal];
[button addTarget:self action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
button.tag=100;
[cell.contentView addSubview:button];
}
then perform action
-(void)aMethod:(id)sender {
NSInteger tag=[[sender superview] tag];
//NSInteger value=[[self.qtt objectAtIndex:tag] integerValue]+1;
//[self.qtt replaceObjectAtIndex:tag withObject:[#(value) stringValue]];
//[self.mytableview reloadData];
}

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;
}

selecting radio button in UITableviewcell in ios

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];
}

Add permanent checkmark to tableView-cell

my viewController contains a tableView where the user can add cells via coreData.
I defined the cells:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Travel";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSManagedObject *travel = [self.travelling objectAtIndex:indexPath.row];
UILabel *countryLabel = (UILabel *)[cell viewWithTag:101];
kategorieLabel.text = [travel valueForKey:#"country"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(15.0f, 32.0f, 24.0f, 20.0f);
[cell addSubview:button];
if ([[travel valueForKey:#"tick"] isEqualToString:#"yes"]) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}else{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
return cell;
}
- (void)buttonTapped:(UIButton *)sender {
UITableViewCell *owningCell = (UITableViewCell*)[sender superview];
[owningCell setAccessoryType:UITableViewCellAccessoryCheckmark];
NSIndexPath *indexPath = [_tableView indexPathForCell:owningCell];
NSManagedObject *travel = [self.travelling objectAtIndex:indexPath.row];
[travel setValue:#"yes" forKey:#"tick"];
}
When the user taps a button on the cell, the checkmark should appear. But when I reload or restart the app not all checkmarks are there. Maybe there is a better method to do this.

Resources