3

I have four buttons in custom cell like ans1, ans2, ans3, ans4 in that i want to validate that buttons in didselectrowatindexpath in each indexpath.row how can i do that? i also set the button tags(1,2,3,4)? here is my cellforatindexpath code

-(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.question.text = [getContestQArray objectAtIndex:indexPath.row];
    NSString *str1 = [getAnswer1Array objectAtIndex:indexPath.row];
    NSString *str2 = [getAnswer2Array objectAtIndex:indexPath.row];
    NSString *str3 = [getAnswer3Array objectAtIndex:indexPath.row];
    NSString *str4 = [getAnswer4Array objectAtIndex:indexPath.row];
    [cell.answer1 setTitle:str1 forState:UIControlStateNormal];
    [cell.answer2 setTitle:str2 forState:UIControlStateNormal];
    [cell.answer3 setTitle:str3 forState:UIControlStateNormal];
    [cell.answer4 setTitle:str4 forState:UIControlStateNormal];
    return cell;
}

if i click on ans1 remaining 3buttons will be deselect if i click on ans2 remaining 3buttons will be deselect and so on ... in each custom cell

rmaddy
  • 314,917
  • 42
  • 532
  • 579
User558
  • 1,165
  • 1
  • 13
  • 37
  • Possible duplicate of [Detecting which UIButton was pressed in a UITableView](http://stackoverflow.com/questions/1802707/detecting-which-uibutton-was-pressed-in-a-uitableview) – evnaz Dec 09 '15 at 12:58
  • It is some how duplicated to the link you provided _evnaz_ but _Mangi_ is doing it in other way like in one cell using four buttons. Please correct if I misunderstood something. – Vijay Sanghavi Dec 09 '15 at 14:13

2 Answers2

1
- (IBAction)select:(id)sender {

    UIButton* btn=(UIButton*)sender;

    CustomCell* cel=(CustomCell*)[btn.superview superview];

    if (cel.a1.tag==btn.tag) {

        [cel.a1 setImage:[UIImage imageNamed:@"active.png"] forState:UIControlStateNormal];
    }else{

        [cel.a1 setImage:[UIImage imageNamed:@"inactive.png"] forState:UIControlStateNormal];
    }
    if (cel.a2.tag==btn.tag) {

        [cel.a2 setImage:[UIImage imageNamed:@"active.png"] forState:UIControlStateNormal];
    }else{

        [cel.a2 setImage:[UIImage imageNamed:@"inactive.png"] forState:UIControlStateNormal];
    }
    if (cel.a3.tag==btn.tag) {

        [cel.a3 setImage:[UIImage imageNamed:@"active.png"] forState:UIControlStateNormal];
    }else{

        [cel.a3 setImage:[UIImage imageNamed:@"inactive.png"] forState:UIControlStateNormal];
    }
    if (cel.a4.tag==btn.tag) {

        [cel.a4 setImage:[UIImage imageNamed:@"active.png"] forState:UIControlStateNormal];
    }else{

        [cel.a4 setImage:[UIImage imageNamed:@"inactive.png"] forState:UIControlStateNormal];
    }

}
Rohit Khandelwal
  • 1,778
  • 15
  • 23
  • @MangiReddy add common IBAction to all four button i.e. -(IBAction)select:(id)sender and then get btn superview like my updated answer- – Rohit Khandelwal Dec 10 '15 at 07:14
  • when i am trying this code in ContestQAViewController.m the buttons it says undeclared identifiers. – User558 Dec 11 '15 at 06:04
  • @MangiReddy can you share your code ? which create this issue – Rohit Khandelwal Dec 11 '15 at 06:20
  • in my above code replace CustomCell with ContestQATableViewCell and change cell answer button with your ContestQATableViewCell answer button.. – Rohit Khandelwal Dec 11 '15 at 06:22
  • that method exist in contestQACustomCell only? – User558 Dec 11 '15 at 07:25
  • please add this method in ContestQAViewController.m and get button using sender id. You can also create @selector() in your -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath and same code will work. – Rohit Khandelwal Dec 11 '15 at 07:33
  • remove IBAction and add same @selector like- [cell.answer1 addTarget:self action:@selector(select:) forState:UIControlStateNormal]; for all four button in ContestQAViewController.m. – Rohit Khandelwal Dec 11 '15 at 07:50
  • [cell.answer1 addTarget:self action:@selector(buttonsClicked:) forState:UIControlStateNormal]; [cell.answer2 addTarget:self action:@selector(buttonsClicked:) forState:UIControlStateNormal]; [cell.answer3 addTarget:self action:@selector(buttonsClicked:) forState:UIControlStateNormal]; [cell.answer4 addTarget:self action:@selector(buttonsClicked:) forState:UIControlStateNormal]; return cell; – User558 Dec 11 '15 at 08:42
  • i am created like this in cellforrowatindexpath? – User558 Dec 11 '15 at 08:50
  • hey @MangiReddy now put above method code in this selector method. add tag to all answer button like 0,1,2,3. My code is working fine – Rohit Khandelwal Dec 11 '15 at 10:04
  • replace my @selector name i.e. select to buttonsClicked, and check sender button tag and cell.ansBtn1.tag in NSLog is matching or not – Rohit Khandelwal Dec 11 '15 at 10:12
  • i am getting this error no visible @interface for uibutton declares the selector addtarget:action:forstate – User558 Dec 11 '15 at 10:29
  • where this error occure i.e. after button click or at the time of viewDidLoad. is selector method is calling ? – Rohit Khandelwal Dec 11 '15 at 10:35
  • Hey @MangiReddy sorry, please change UIControlStateNormal to UIControlEventTouchUpInside in addTarget like - [cell.answer1 addTarget:self action:@selector(buttonsClicked:) forControlEvents:UIControlEventTouchUpInside]; – Rohit Khandelwal Dec 11 '15 at 10:54
  • Thank you :) it's working fine. tanq for your valuable time – User558 Dec 11 '15 at 10:56
  • Welcome dear.. if the your problem is resolved please feel free to accept the answer so that it can help other users. – Rohit Khandelwal Dec 11 '15 at 10:59
  • ya sure:) i am a fresher that's y i am little bit confusing thank you once again:) – User558 Dec 11 '15 at 11:02
  • Not an issue Dear @MangiReddy – Rohit Khandelwal Dec 11 '15 at 11:09
  • when i am selecting button like ans1 in cell1 and selecting ans2 in another cell2 ! after that i scrolling my view the ans1 will be deselecting? – User558 Dec 11 '15 at 12:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97643/discussion-between-user5097148-and-mangi-reddy). – Rohit Khandelwal Dec 11 '15 at 13:10
1

I see your implement now is not good. Have the way to get it but it's not clear. So I suggest you should put action of button action to class of ContestQATableViewCell and call delegate back to controller have table view and doSomeThingWithit

First you create delegate:

@protocol ContestCellDelegate <NSObject>

@required
- (void)didTapButtonAtIndexpath:(NSIndexPath *)indexPath posision:  (NSInteger)position;
@end
  @interface ContestQATableViewCell : UITableViewCell

 @property (weak, nonatomic) IBOutlet UILabel *question;
 @property (weak, nonatomic) IBOutlet UIButton *answer1;
 @property (weak, nonatomic) IBOutlet UIButton *answer2;
 @property (weak, nonatomic) IBOutlet UIButton *answer3;
 @property (weak, nonatomic) IBOutlet UIButton *answer4;

 @property (strong, nonatomic) NSIndexPath *indexPath;
 @property (weak, nonatomic) id<ContestCellDelegate>delegate;

 //- (void)configCellWithQuestion:()


  @end

And you add action in Cell when user tap it you call delgate back:

 - (IBAction)answer1IsTapped:(id)sender {
if (self.delegate) {
    [self.delegate didTapButtonAtIndexpath:self.indexPath posision:1];
   }
}
 - (IBAction)answer2IsTapped:(id)sender {
if (self.delegate) {
    [self.delegate didTapButtonAtIndexpath:self.indexPath posision:2];
}
}

   - (IBAction)answer3IsTapped:(id)sender {
if (self.delegate) {
    [self.delegate didTapButtonAtIndexpath:self.indexPath posision:3];
}
}

   - (IBAction)answer4IsTapped:(id)sender {
if (self.delegate) {
    [self.delegate didTapButtonAtIndexpath:self.indexPath posision:4];
}
 }

And in controller you catch and do what you want:

    - (void)didTapButtonAtIndexpath:(NSIndexPath *)indexPath posision:(NSInteger)position {
//You can get index postion and do anything here
NSLog(@"%ld %ld", (long)indexPath.row, (long)position);
}

More detail you can get this code and apply to your project: Demo Code

vien vu
  • 4,277
  • 2
  • 17
  • 30
  • sorry i am already created ContestQACustomCell(.h and .m) in that i also created properties for Label n Buttons – User558 Dec 10 '15 at 05:50
  • You can do like the way I do. It just demo, you can drag outlet and action and hale same way. – vien vu Dec 10 '15 at 06:11
  • when i am scrolling my table view it is reloading the data, how can i stop it? – User558 Dec 16 '15 at 04:53
  • What is reloading? Can you describe more clearly? what is function reload? When you call, How you call it? – vien vu Dec 16 '15 at 04:58
  • i have a tableview in that i am created one custom cell in that i have a question label and four answer buttons, in that i am clicking on my 1st question ans1 button and 2nd question 3rd button after that i scrolling my cell it is deallocating what i am selected? – User558 Dec 16 '15 at 05:22
  • @MangiReddy ah I see, I think problem here is you dont' save selected andswer to your model or something like this. You should keep it and in `cellForRowAtIndexPath` you config cell depend on this data. If you do like this data will keep and don't change when you scroll. – vien vu Dec 16 '15 at 06:42