1

I have a grouped table view in my application with two sections.First one consist of one row and the second one with 5.I have added buttons to the cells,like this,`

   UIButton *newBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [newBtn setFrame:CGRectMake(5,10,35,30)];
    [newBtn setImage:[UIImage imageNamed:@"icon.png"] forState:UIControlStateNormal];
    [newBtn addTarget:self action:@selector(selector:)        forControlEvents:UIControlEventTouchUpInside];
    newBtn.tag=4;
    [cell.contentView addSubview:newBtn];

Now in that selector methode i need to change it with another image.I am trying to do that in this way

 UITableViewCell *cell=(UITableViewCell *)[sender superview];

    NSIndexPath *path=[self.mtableview indexPathForCell:cell];

`But bottle necked at some point.Can any body point me in the wright direction?

angry_developer
  • 215
  • 1
  • 11

2 Answers2

3

Add this in your selector if you want to change image of button alone.

- (void)selector:(id)sender {
  //some code
  UIButton *button = (UIButton *)sender;
  [button setImage:[UIImage imageNamed:@"newicon.png"] forState:UIControlStateNormal];
  //some code
}
iDev
  • 23,310
  • 7
  • 60
  • 85
  • I need to change the button in the corresponding cell only.all others needs to be the same – angry_developer Oct 31 '12 at 06:40
  • 1
    Yes, this will do that. You have different buttons in different cells right? try it out and let me know. Or do you mean to say that if you tap on any other buttons in cells, those shouldnt change even though they are different buttons? – iDev Oct 31 '12 at 06:41
  • No..all r same button.that icon.png.when clicking on it the background image of the clicked button needs to be changed – angry_developer Oct 31 '12 at 06:43
  • when selecting one button it should have to deselect all others?can u help me? – angry_developer Oct 31 '12 at 06:58
  • So do you mean to say that what you exactly want is, kind of a checkmark implementation where when we select one, others need to be deselected? Try searching for checkmark and tableview and you can get a lot of sample code for that from SO or google. You can use the same implementation here. – iDev Oct 31 '12 at 07:01
  • @angry_developer, Here is my answer to another post regarding one such implementation. Check if you can implement the same logic here. http://stackoverflow.com/a/13056452/1730272 – iDev Oct 31 '12 at 22:18
2

You Should Write Your Action Method For Button Like this way

-(void)cellBtnAction:(id)sender {

    UIButton *btn=(UIButton *)sender;
    //Write code for change image to button
    [btn setImage:imgObj forState:UIControlStateNormal];
}

Try This I hope it will helps You

Raheel Sadiq
  • 9,847
  • 6
  • 42
  • 54
KAREEM MAHAMMED
  • 1,675
  • 14
  • 38