2vision2
2vision2

Reputation: 5023

Change a button dynamically in a table view cell

I have a table where 5 cells are there. In each cell there is a button. I need to change those buttons dynamically on some conditions. How to change the button dynamically in table view.

Upvotes: 0

Views: 577

Answers (3)

Saravana Vijay
Saravana Vijay

Reputation: 572

First you need to set tags for each buttons. This can be done by

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Your code
cell.Button.tag = indexPath.row;
[cell.Button addTarget:self action:@selector(buttonClick1:) forControlEvents:UIControlEventTouchUpInside];

}

Then you can get the button from this method

- (IBAction)buttonClick1:(id)sender {

int tagNo =[sender tag];
UITableViewCell *cellclicked = [self.tblProfileFollowing cellForRowAtIndexPath:[NSIndexPath indexPathForRow:tagNo inSection:0]];
//Now change the action for the cell
[cellclicked.Button addTarget:self action:@selector(buttonClick2:) forControlEvents:UIControlEventTouchUpInside];

//Your Code

}

You can follow the same steps for - (IBAction)buttonClick2:(id)sender

Now you can track which button was clicked and change the method for that button.

And declare your cell and button strong.

Upvotes: 0

Igor
Igor

Reputation: 4838

You can query your table view for all visible cells through -visibleCells, get the reference of your button (assuming you have a UITableViewCell subclass with a property for the button) and change them.

Upvotes: 1

Rajan Balana
Rajan Balana

Reputation: 3815

What you can do is Write a function which will change the values of the buttons and then you can call that function. After changing button values then use any one of the mentioned method below:

You can use either : [tableview reloadData]; to reload all the table data.

OR

You can reload particular rows using the following method :

[tableview reloadRowsAtIndexPaths: indexPath withRowAnimation:anyKindOfAnimation];

Upvotes: 1

Related Questions