sujith1406
sujith1406

Reputation: 2822

need to refresh a uilabel programmatically in a tableview when slider is changed

i have a tableview in which there is a uislider at section 2 and row 3 and a label in the same place,i need to change the value of uilabel as the uislider is changed . i have given method for uislider like

[slider addTarget:self action:@selector(sliderAction:)forControlEvents:UIControlEventValueChanged];             

and for uislider i have given

-(void)sliderAction:(UISlider*)sender;

{   
    NSLog(@"%f",[sender value]);

    MlifetimeLabel.text=[NSString stringWithFormat:@"%f",[sender value]];
    [self.tableView reloadData];


}

in nslog i am getting a continous stream of values but in the label......things are not getting refreshed properly........help

Upvotes: 1

Views: 1110

Answers (2)

Sorin Antohi
Sorin Antohi

Reputation: 6135

you will need to update the label on the main thread. try something like

NSString *str = [NSString stringWithFormat:@"%f",[sender value]];

[MlifetimeLabel performSelectorOnMainThread : @ selector(setText : ) withObject:str waitUntilDone:YES];

Upvotes: 1

Robin
Robin

Reputation: 10011

You dont need to reload the whole table as you know the row and section of the cell that you have to change just try this code in the selector method.

[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:3 inSection:2]] 
                      withRowAnimation:UITableViewRowAnimationNone];

I am assuming that you are counting the row and section from 0 index not 1 otherwise use

[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:2 inSection:1]] 

Upvotes: 1

Related Questions