0

I have a string that has a value that keeps on changing/updated and I want to kind of bind this to my cell.textLabel.text. How can I do this?

This is similar to the datepicker that we have on the iphone... the cell is updated everytime we change the picker view value

Here's how I had it laid out:

@interface DatePickerViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    UITableView * date;
    UIDatePicker * datePicker;
    NSString * dateString;
}

@property(nonatomic, retain) IBOutlet UITableView * date;
@property(nonatomic, retain) IBOutlet UIDatePicker * datePicker;


- (IBAction)dateChangedAction:(id)sender;

@end
2
  • how does the string change? You need to provide more information Nov 7, 2010 at 20:14
  • the string is a string that represents the date that user chooses from the date picker... Nov 7, 2010 at 20:20

2 Answers 2

1

Since you are using the Date Picker

// when creating datePicker object
[datePicker addTarget:self action:@selector(changeDateInCell:)
     forControlEvents:UIControlEventValueChanged];

Create method to respond to event

- (void)changeDateInCell:(id)sender{
     //Use NSDateFormatter to get string
     NSDateFormatter *df = [[NSDateFormatter alloc] init];
     df.dateStyle = NSDateFormatterMediumStyle;
     NSString * text = [NSString stringWithFormat:@"%@", 
                                        df stringFromDate:datePicker.date]];

     // do something to set the cell data with the text...

     // clean up
     [df release];

}

4
  • I got all the code above actually even before posting my problem.. my question is how to set the cell data with the text in that method changeDateInCell? how can you get the cell from that method? Nov 7, 2010 at 22:57
  • is it a fixed cell? There is not enough code provided in the question to answer that question Nov 7, 2010 at 23:48
  • the code is exactly like you have above... if you can fill in the //do something to set the cell data with text, then that answers my question Nov 8, 2010 at 3:13
  • how is the table set up? Where is the table located? in what controller? You are asking for an answer and there is no information provided to allow someone to help you... you have put a single line of code up here but you expect an very specific answer Nov 8, 2010 at 4:04
0

If the string is a property of some object (or can be exposed as such), I'd use a Key Value Observer (KVO). Using addObserver:forKeyPath:options:context: you can get a callback (observeValueForKeyPath:ofObject:change:context:) when the value of your property changes, and update the cell accordingly. If your cell is a custom cell you could wrap the functionality within the cell, otherwise you could place it in your view controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.