Reputation: 185
I have to change this "Follow" Button to "Following" on clicking each cell and when I click again on "Following",it should go back to "Follow". How is it possible?
Upvotes: 0
Views: 429
Reputation: 359
It's quite simple i guess. In your tableview didSelectRowAtIndexPath delegate method you just have to set your button color and title like this.
[buttonName setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[buttonName setTitle:@"Following" forState:UIControlStateNormal];
and when you click again on that same cell, you have to check button text . You can do that by setting a boolean value. i.e when you select a particular cell set boolean value to YES and set button text to "Following" and on clicking on the same cell again check if boolean is set or not if it is set you can change the text to "Follow". Other option is you have to store the selected indexpath in an mutable array and on each selection of cell try to push and pop that object in an array.. Here is other step
First create an Mutablearray in you .h File
@property (strong,nonatomic) NSMutableArray *selectedIndexPaths;
Alloc in it in your viewDidLoad method.
self.selectedIndexPaths = [[NSMutableArray alloc]init];
Add this method in you .m file
- (void)addOrRemoveSelectedIndexPath:(NSIndexPath *)indexPath
{
BOOL containsIndexPath = [self.selectedIndexPaths containsObject:indexPath];
if (containsIndexPath) {
[self.selectedIndexPaths removeObject:indexPath];
}else{
[self.selectedIndexPaths addObject:indexPath];
}}
Now in your didSelectRowAtIndexPath delegate method add this method to check if that array contains your selected indexpath.
[self addOrRemoveSelectedIndexPath:indexPath]//call of the method;
BOOL isSelected = [self.selectedIndexPaths containsObject:indexPath];
if(isSelected)
{
//change your button text and color.
}else{//unset your button text and color..}
In this way you can easily manage your indexpath of cell. regardless of getting conflict on it..
Upvotes: 0
Reputation: 8782
Create action from cell to controller. now you have reference to your cell button
-(void)followButtonActionFromTableViewCell:(UIButton *)sender
{
[self followUnfolloWithButton:sender]
}
-(void)followUnfolloWithButton:(UIButton *)sender{
//you may need to call web service and set button accordingly
[sender setTitle:@"Follow" forState:UIControlStateNormal];
[sender setBackgroundColor:[UIColor yellowColor]];//set your colour
}
Use if you want reload tableview.
UIButton * button = (UIButton*)sender;
CGRect frameRect ;
NSIndexPath * indexPath ;
frameRect=[button convertRect:button.bounds toView:self.tableView];
indexPath= [self.tableView indexPathForRowAtPoint:frameRect.origin];
//Reload
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationAutomatic];
To save state of follow/following put check condition in cellForRowAtIndexPath method. and
update your model by sender.tag
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//...your code
cell.yourFollowButton.tag=indexPath.row;
//check condition and set button text and colour
}
Upvotes: 1
Reputation: 910
- (IBAction)btnStartOnClick:(id)sender {
UIButton *someButton = (UIButton*)sender;
NSString *str=[someButton titleForState:UIControlStateNormal];
if ([str isEqualToString:@"Follow"])
{
[btn setTitle:@"Following" forState:UIControlStateNormal];
}
else if ([str isEqualToString:@"Following"])
{
[btn setTitle:@"Follow" forState:UIControlStateNormal];
}
Upvotes: 0
Reputation: 113
the question is when yo click the button and change the button's some property, maybe you reload the tableView or reload cell. the status didn't cache .
the button's color and text shold obsver user model's property (eg: followStatus), when click the button , yo should change de model's followStatus and reload the cell.
thanks.
Upvotes: 0