2010-11-07 69 views
0

我有一個字符串,其值不斷更改/更新,我想這樣綁定到我的cell.textLabel.text。我怎樣才能做到這一點?NSString和一個TableView單元格文本

這是類似的,我們有iPhone上的日期選擇器...細胞更新,每次我們改變選擇器視圖值

這裏是我怎麼過的佈局:

@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 
+0

怎麼做字符串變化?您需要提供更多信息 – 2010-11-07 20:14:17

+0

字符串是一個字符串,表示用戶從日期選擇器中選擇的日期... – aherlambang 2010-11-07 20:20:38

回答

1

由於您使用日期選取器

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

Create方法來響應事件

- (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]; 

}

+0

在發佈我的問題之前,我已經獲得了上述所有代碼......我的問題是如何設置單元格數據用該方法中的文本changeDateInCell?你怎麼能從這種方法獲得細胞? – aherlambang 2010-11-07 22:57:29

+0

它是一個固定的細胞?沒有足夠的代碼提供的問題來回答這個問題 – 2010-11-07 23:48:05

+0

代碼是完全一樣的你上面...如果你可以填寫/ /做一些事情來設置單元格數據與文本,然後這回答我的問題 – aherlambang 2010-11-08 03:13:57

0

如果該字符串是某個對象的屬性(或者可以像這樣暴露),我會使用Key Value Observer(KVO)。使用addObserver:forKeyPath:options:context:當屬性值發生變化時,您可以獲得回調(observeValueForKeyPath:ofObject:change:context :),並相應地更新單元格。如果您的單元格是自定義單元格,則可以將單元格中的功能封裝起來,否則可以將其放入您的視圖控制器中。

相關問題