User558 User558 - 6 months ago 37
iOS Question

How to save the clicked button indexpath in custom cell?

Here is my problem, i have a custom cell in that 1 label and 4 buttons like question and answers,when the user click on the button 1 remaining 3 button go like this,
enter image description here

When the user scroll the cell it is not saving the what the user selected button i mean to say user click on the what ever the button it is not saving.
Here is my code,

ViewDidLoad()
{
testArray = [[NSMutableArray alloc]init];
for (int i =0; i<[mainArray count]; i++)
{
[testArray addObject:@"Unchecked"];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = @"Cell";
ContestQATableViewCell *cell =(ContestQATableViewCell *)[tableViewQA dequeueReusableCellWithIdentifier:cellId];
if (cell==nil)
{
NSArray *myNib;
myNib =[[NSBundle mainBundle]loadNibNamed:@"ContestQATableViewCell" owner:self options:nil];
cell = (ContestQATableViewCell *)[myNib lastObject];
}
cell.textLabel.text = [mainArray objectAtIndex:indexPath.row];
if([[testArray objectAtIndex:indexPath.row] isEqualToString:@"Unchecked"])
[cell.answer1 setImage:[UIImage imageNamed:@"RadioUnChecked"] forState:UIControlStateNormal];
else
[cell.answer1 setImage:[UIImage imageNamed:@"RadioChecked"] forState:UIControlStateNormal];
cell.answer1.tag = indexPath.row;
[cell.answer1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

[cell.contentView addSubview:cell.answer1];

return cell;

[cell.answer1 addTarget:self action:@selector(buttonsClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.answer2 addTarget:self action:@selector(buttonsClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.answer3 addTarget:self action:@selector(buttonsClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.answer4 addTarget:self action:@selector(buttonsClicked:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}


and here is my add target method

-(void)buttonsClicked:(id)sender
{
UIButton *btn=(UIButton *)sender;
ContestQATableViewCell * cell=(ContestQATableViewCell *) [btn.superview superview];
if (cell.answer1.tag==btn.tag)
{
[cell.answer1 setImage:[UIImage imageNamed:@"RadioChecked"] forState:UIControlStateNormal];
}else{

[cell.answer1 setImage:[UIImage imageNamed:@"RadioUnChecked"] forState:UIControlStateNormal];
}
if (cell.answer2.tag==btn.tag)
{
[cell.answer2 setImage:[UIImage imageNamed:@"RadioChecked"] forState:UIControlStateNormal];
}else{

[cell.answer2 setImage:[UIImage imageNamed:@"RadioUnChecked"] forState:UIControlStateNormal];
}
if (cell.answer3.tag==btn.tag)
{
[cell.answer3 setImage:[UIImage imageNamed:@"RadioChecked"] forState:UIControlStateNormal];
}else{

[cell.answer3 setImage:[UIImage imageNamed:@"RadioUnChecked"] forState:UIControlStateNormal];
}
if (cell.answer4.tag==btn.tag)
{
[cell.answer4 setImage:[UIImage imageNamed:@"RadioChecked"] forState:UIControlStateNormal];
}else{

[cell.answer4 setImage:[UIImage imageNamed:@"RadioUnChecked"] forState:UIControlStateNormal];
}
}

Answer

For Check-Uncheck functionality only buttonClicked: method is not enough. You will have also put the condition in cellForRowAtIndexPath: method for which button is selected or which in unselected because cellForRowAtIndexPath: method will call each time when you will scroll your UITableView and cells will be refresh.

for example

I explain in step by step

Step-1

Create the two arrays one for gloabally another one for checking purpose

@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
{

    NSMutableArray *arrayforCheckUnchek; // handle which button is selected or which is unselected
    NSMutableArray *originalArray; // original Array
}

Step-2

allocating the memory for arrays

- (void)viewDidLoad
{
    [super viewDidLoad];


    arrayforCheckUnchek = [[NSMutableArray alloc]init];

    originalArray = [[NSMutableArray alloc]initWithObjects:@"cell1",@"cell2",@"cell3",@"cell4",@"cell5", nil];

    // setting  all cell initilayy at un check
    for(int i=0; i<[originalArray count]; i++)
    {
        [arrayforCheckUnchek addObject:@"Unchecked"];
    }

   }

Step-2

setup your datasource Methods

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [originalArray count];
}

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *Identifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [originalArray objectAtIndex:indexPath.row];

    UIButton *radiobutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [radiobutton setFrame:CGRectMake(270.0, 7.0, 30.0, 30.0)]; // customize the frames

    if([[arrayforCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Unchecked"])
    [radiobutton setImage:[UIImage imageNamed:@"RadioUnChecked"] forState:UIControlStateNormal];
    else
    [radiobutton setImage:[UIImage imageNamed:@"RadioChecked"] forState:UIControlStateNormal];
    radiobutton.tag = indexPath.row;
    [radiobutton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

    [cell.contentView addSubview:radiobutton];

    return cell;
 }

Step-3

here selected button index you can change the image

 -(void)buttonClicked:(UIButton *)sender
{
    //Getting the indexPath of cell of clicked button

    CGPoint touchPoint = [sender convertPoint:CGPointZero toView:yourtableviewname];
    NSIndexPath *indexPath = [yourtableviewname indexPathForRowAtPoint:touchPoint];

    //Checking the condition button is checked or unchecked.

    if([[arrayforCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Unchecked"])
    {
        [sender setImage:[UIImage imageNamed:@"RadioChecked"] forState:UIControlStateNormal];
        [arrayforCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Checked"];
    }
    else
    {
        [sender setImage:[UIImage imageNamed:@"RadioUnChecked"] forState:UIControlStateNormal];
        [arrayforCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Unchecked"];
    }
    }