3

I add UIButton in the cell Dynamically and change UIButton image on click of UIBarButtonItem how i can change?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Display"] ; 
    [cell.textLabel setText:[NSString stringWithFormat:@"%@" ,[arrItemList objectAtIndex:indexPath.row]]];

    UIButton *btnCheck = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnCheck.frame = CGRectMake(6.0f, 2.0f, 32.0f, 25.0f);
    [btnCheck setTitle:@"" forState:UIControlStateNormal]; 
    [btnCheck addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:btnCheck]; 

    return cell;
}
//Here IBAction for Bar Button Item
//when ever user click on BarButton then set the image of all btnCheck
-(IBAction)bbtnCheck:(id)sender
{
}
MadhuP
  • 2,019
  • 17
  • 22
Mihin
  • 320
  • 1
  • 15
  • Can you show me your code, what have you tried or made till now? – Anoop Vaidya Jan 01 '13 at 08:30
  • ok i already put it.u can see that – Mihin Jan 01 '13 at 08:43
  • no action? no image to show on button? – Anoop Vaidya Jan 01 '13 at 08:46
  • [bttnCheck setImage:unselectedImage forState:UIControlStateNormal]; – Anoop Vaidya Jan 01 '13 at 08:47
  • i dont need to any action and any image at first time but when user click on Bar Button at that time i wont to set image on this all button which is generated dynamically. – Mihin Jan 01 '13 at 08:52
  • Note that you're not allowing proper cell reuse since you're allocating a new button for a cell each time a cell is displayed. Secondly, you generally want to add cell subviews to the cell's contentView, not the cell itself. – Bill Jan 01 '13 at 09:22
  • check this link http://stackoverflow.com/questions/650131/checkbox-in-iphone-application – Paras Joshi Jan 01 '13 at 09:22
  • Are you attempting to change every cell's button or a specific cell's button when the bar button item is clicked? – Bill Jan 01 '13 at 09:31
  • for all UIButton which is in cell – Mihin Jan 01 '13 at 09:51
  • i try your code but till i am not able to set image on Button if i can set tag value at starting of loop then it will set only first button – Mihin Jan 01 '13 at 11:33
  • The tag is set when the cell is created/dequeued. That line of code (i.e., btnCheck.tag = MY_BUTTON_TAG_NUMBER) should be in tableView:cellForRowAtIndexPath, which is called each time a cell is needed. – Bill Jan 01 '13 at 19:59

2 Answers2

1

Assuming that your question is how to change the image of a button w/in every cell of your table when a bar button is clicked, I suggest the following:

Within tableView:cellForRowAtIndexPath, add:

btnCheck.tag = MY_BUTTON_TAG_NUMBER;

Then, when the bar button item is clicked, loop through the visible cells and use the button's tag to identify it and update its image.

-(IBAction)bbtnCheck:(id)sender {
 for (UITableViewCell *cell in self.tableView.visibleCells) {
   UIButton *button = [cell viewWithTag:MY_BUTTON_TAG_NUMBER];
   [button setImage:[UIImage imageNamed:SOME_NEW_IMAGE] forState:UIControlStateNormal];
  }
}
Bill
  • 311
  • 1
  • 10
0

In that bbtnCheck you have to get the instance of that tableView (i.e. particular cell)..Then after you can get buttons added on that particular cell.. Please Check this....

-(IBAction)bbtnCheck:(id)sender
{
     NSIndexPath *indexpath = [NSIndexPath indexPathForRow:YourRow inSection:YourSection]; // you have to set Row and Section according to your requirement
     UITableViewCell *cellNew = [self.tblComments cellForRowAtIndexPath:indexpath];
    for (UIButton *btn in [cellNew.contentView subviews]) 
    {
        if ([btn isKindOfClass:[UIButton class]]) 
        {
            [btn setImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal];
        }
    }    
}
Murali
  • 1,869
  • 1
  • 14
  • 22
  • 1
    You won't be able to reference the button w/in the cell like that. – Bill Jan 01 '13 at 09:04
  • how i can access btnCheck in this method is it possible ?? – Mihin Jan 01 '13 at 09:12
  • @user1934408: use custom cell and add button into custom cell and give target to button in cellAtIndex... method, then you can get access like above... – Maulik Jan 01 '13 at 09:15
  • Looping through all subViews is wasteful and not specific. Just tag the button subview, as shown above. – Bill Jan 01 '13 at 09:32
  • That's one time too many already. And that number grows if/when other subviews are added in the future. You're also assuming that all buttons w/in the cell have the same behavior. – Bill Jan 01 '13 at 09:40