0

I am having a table view, in the table cell I have two buttons and checking their conditions. its working but my problem is I want to check the button condition for every single cell in the table view. can anyone help me please.

button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(80, 27, 36, 36);
[button1 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"l"ofType:@"png"]] forState:UIControlStateNormal];
button1.tag = indexPath.row;
[button1 addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button1];

button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.frame = CGRectMake(160, 27, 36, 36);
[button2 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"e"ofType:@"png"]] forState:UIControlStateNormal];
button2.tag = indexPath.row;
[button2 addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button2];

- (void)radiobtn:(UIButton *)button
{
  if(btn3 == 0) {
if ([button isSelected]) {
    [button setImage:[UIImage imageNamed:@"l.png"]
            forState:UIControlStateNormal];
[[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"button1"];
 [button setSelected:NO];
} else {
    [button setImage:[UIImage imageNamed:@"lblue.png"]
            forState:UIControlStateSelected];
[button setSelected:YES];
else{
}
}
- (void)radiobtn3:(UIButton *)button
{


if(btn1 == 0)
{
if ([button isSelected]) {
    [button setImage:[UIImage imageNamed:@"v.png"]
            forState:UIControlStateNormal];
    [[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"button3"];

}


 [button setSelected:NO];
} else {
    [button setImage:[UIImage imageNamed:@"vblue.png"]
            forState:UIControlStateSelected];
  [button setSelected:YES]; }}
else {
}

conditions are working good. but i want to check the conditions for every single cell . help me in coding.

Raj
  • 67
  • 2
  • 9
  • 1
    You should not keep the state, which you are checking, in the cell. Since cell's are reused they are only representing the data that fills then. So you state should be stored in the object that fill the cell. – rckoenes Mar 03 '15 at 10:49
  • @rckoenes i am new to ios can you help me in coding – Raj Mar 03 '15 at 10:57

2 Answers2

0

You need to set tags for every button you allocate depending on your indexPath.row in cellForRowAtIndexPath datasource method of your UITableView. This link might help you out.

Community
  • 1
  • 1
Deepak Thakur
  • 3,453
  • 2
  • 37
  • 65
  • i tried this is not helping,can u help me in coding – Raj Mar 03 '15 at 10:55
  • 1
    you need to learn about basics of UITableView and its respective datasource and delegate methods before jumping into making objective c apps.. Go through the tutorials please.. – Deepak Thakur Mar 03 '15 at 10:59
0

Maintain one array for checking the state of UIButton which will be class member.

Add #define for each button to know uniquely which button state we are referring like this :

#define kFirstBtnState @"firstbuttonstate"
#define kSecondBtnState @"secondbuttonstate"

Now initialize array in viewDidLoad like this :

//use tableview object array here 
for(int i=0; i<[yourDataSourceOfTableViewArray count]; i++)
{
   //Currently each button state is 0 which is false
   NSDictionary *dictBtnState = [NSDictionary dictionaryWithObjectsAndKeys:@"0",kFirstBtnState,@"0",kSecondBtnState,nil];
   [mutArrBtnStateMaintain addObject:dictBtnState];
}

Use created array in UITableView's cellForRowAtIndexPath like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   ....................
   ....................

   //get state list of buttons
   NSDictionary *dictBtnState = [mutArrBtnStateMaintain objectAtIndex:indexPath.row];
   BOOL firstBtnState = [[dictBtnState objectForKey:kFirstBtnState] boolValue];
   BOOL secondBtnState = [[dictBtnState objectForKey:kSecondBtnState] boolValue];

   //Act according to your requirement

   return cell;
}

Note : Replace state in array when necessary to maintain the state of UIButton

Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132