2012-08-01 31 views
1

我已經創建了一個使用故事板的靜態UITableView。表格視圖中的每一行都包含一個文本字段,並且每行都有一個標識符。其中一個文本框用於輸入日期。我想在選擇此文本字段時顯示UIDatePicker。我試圖將文本字段的輸入視圖設置爲UIDatePicker來完成此操作。我正努力在這個特定的UITableViewCell上獲取UITextField的位置。所以這裏是我要去的方向:如何從UITableViewCell獲取對UITextField的引用?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    const NSString *birthDateCellIdentifier = @"BirthDateCell"; 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    NSString * cellIdentifier = [cell reuseIdentifier]; 
    if(cellIdentifier isEqualToString:birthDateCellIdentifier){ 
     /*this is the cell we're after so get pointer to cell's textfield*/ 
     //set textfield's inputview to UIDatePicker 

    } 


} 

這是正確的做法嗎?如果是的話,我如何找到這個單元格上的文本字段?

謝謝!

回答

1

不,事實並非如此。如果您爲每個單元格使用不同的重複使用標識符,則表格wiev將無法重新排列單元格,因此您將浪費內存。改用tag屬性。

如果您發現該單元格,您必須將文本字段添加爲一個屬性吧,這是訪問它最徹底的方法。

+0

謝謝。我在IB(storyboard)中創建了單元格。那麼文本字段不應該是該單元格的屬性了嗎? – Nick 2012-08-01 22:51:48

+1

如果你沒有在代碼中聲明它(或者它不在那裏),那麼不是。 (這就是爲什麼我一般不鼓勵使用IB作爲新手 - 這是**令人困惑**。) – 2012-08-01 22:53:52

+0

這是令人困惑的。我會給它一個 – Nick 2012-08-01 22:55:22

0

你有沒有考慮到的UITableView是一個糟糕的選擇的可能性對於這個特殊的問題?

想想吧 - 每個「細胞」似乎外觀和行爲不同,實現不同的目的,並有他們的固定數有沒有必要進行動態數據綁定。

如果我是你,我只想讓每個輸入字段的自定義視圖和放下在一個UIScrollView整個事情。

即使你是在這種方法中踏實,有一個問題。 UITableViewCells不具有UITextField屬性,因此您將無法訪問它們。就像這樣做一個類:

@interface DatePickerView : UIView <UITextFieldDelegate> 

@property (nonatomic, retain) UITextField *textField; 
@property (nonatomic, retain) UIDatePicker *datePicker; 

@end 

現在所有的邏輯都包含在DatePickerView類中。您可以實現UITextFieldDelegate方法來處理輸入,而不必擔心所選的文本字段。在執行DatePickerView時,你可以這樣做:

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    // Show the UIDatePicker. 
} 

這會給你一個更清潔和更好的包含設計。

1

可以使用indexPath用於設置UITextFieldinputView財產UIDatePicker

這段代碼可以幫助你...

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *identifier = @"Identifier"; 
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:identifier]; 
    if (cell == nil) { 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 
    } 

    UITextField *textField = [[[UITextField alloc] initWithFrame:CGRectMake(10, 10, 200, 20)] autorelease]; 
    textField.textColor = [UIColor darkTextColor]; 
    textField.tag = indexPath.row; 
    textField.delegate = self; 
    [cell.contentView addSubview:textField]; 

    if (indexPath.row ==0) { 
     // Setting Datepicker as a inputView to the textfield on first ell 
     UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease]; 
     [datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged]; 
     textField.inputView = datePicker; 
    }else if(indexPath.row == 1) { // Set the Number pad for the textfield on second cell 
     textField.keyboardType = UIKeyboardTypeNumberPad; 
    } 

    return cell; 
} 


// DatePicker selector 
-(void)dateChanged:(id)sender 
{ 
    UIDatePicker *picker = (UIDatePicker *)sender; 
    NSDate *date =picker.date; 
    NSLog(@"selected date: %@", date); 
} 



// TextField Delegate methods 
-(void)textFieldDidBeginEditing:(UITextField *)textField 
{ 

} 
+0

,所以這很好,但是,在dateChanged方法中,如何獲取對textField的引用以將其文本設置爲所選日期? – 2015-03-16 22:47:31

相關問題