I have a UIButton in my UITableViewCell. This button launches a UIActionSheet so when a user pics a button from the action sheet, I want the text of the button to change to that text.
I show the button with the following code. I think if I call ‘tableView reload’ in actionSheet: clickedButtonAtIndex: would work but is there a better alternative to just update the statusButton?
SmokingStatusTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.statusButton.titleLabel.text = [self.vitalsDictionary objectForKey:@"smoking_status_display"];
- (IBAction)smokingStatusButtonClicked:(id)sender{
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"Smoking Status" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"1 Current everday smoker", @"2 Current some day smoker", nil];
self.smokingStatusActionSheet = actionSheet;
[self.smokingStatusActionSheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (actionSheet == self.smokingStatusActionSheet){
switch (buttonIndex) {
case 0:
[self.vitalsDictionary setObject:@"daily_smoker" forKey:@"smoking_status"];
break;
case 1:
[self.vitalsDictionary setObject:@"nondaily_smoker" forKey:@"smoking_status"];
break;
default:
break;
}
}
}
You should update your data source and then you can potentially reconfigure the cell again if required.
If the cell is off screen then it should automatically be updated in your implementation of
tableView:cellForRowAtIndexPath:when it becomes visible again.If you want to force it to update and it is visible you can:
Call
reloadDataCall
reloadRowsAtIndexPaths:withRowAnimation:Call
cellForRowAtIndexPath:and configure it.For 2 + 3 you will need to keep a reference to the indexPath of the cell that invoked the action sheet.
If you choose 3 it may help to have your
tableView:cellForRowAtIndexPath:method set up like so:By separating out
configureCell:atIndexPath:you can use the same code to configure the cell.For example you may call