Reputation: 14418
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
Upvotes: 0
Views: 399
Reputation: 33345
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];
}
Upvotes: 1
Reputation: 39512
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.
Upvotes: 0