0

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];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • check Whether you are assigning different tags to button in every Cell and access button with the assigned tag in the LOOP not out of the loop, set its background image in the loop – Muhammad Waqas Bhati Aug 10 '15 at 06:45
  • I have created the Butoon in storyboard using custom cell. and all buttons have tag 1 as they are in table view cell @MuhammadWaqasBhati – user5185785 Aug 10 '15 at 06:47
  • What is the 'for' loop for, and where are you storing the selection? – Wain Aug 10 '15 at 06:56
  • @Wain for loop is for to set all radio boxes to unchecked and when its done i am finaaly assignng checked value to selected option. And currently i am not storing the selecion ..it just an UI – user5185785 Aug 10 '15 at 07:37
  • Your for loop sets the same button to deselected many times... – Wain Aug 10 '15 at 09:17

4 Answers4

0

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.

Muhammad Waqas Bhati
  • 2,775
  • 20
  • 25
  • it doesn't work... crashes with giving the following error :'Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setBackgroundImage:forState:]: unrecognized selector sent to instance 0x7fdbca5b1a50' – user5185785 Aug 10 '15 at 07:37
  • I have made an edit to my code kindly check it Now it will work – Muhammad Waqas Bhati Aug 10 '15 at 07:59
0

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.

Raman soni
  • 86
  • 4
0

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.

0

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

Rahul
  • 5,594
  • 7
  • 38
  • 92