1

我已经使用情节提要创建了一个静态 UITableView。表格视图中的每一行都包含一个文本字段,并且我为每一行指定了一个标识符。其中一个文本字段用于输入日期。我想在选择此文本字段时显示 UIDatePicker。我正在尝试将文本字段的 inputview 设置为 UIDatePicker 来完成此操作。我正在努力获取这个特定 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

    }


}

这是正确的方法吗?如果是这样,我如何在此单元格上找到文本字段?

谢谢!

4

4 回答 4

2

您可以使用indexPath用于设置as的inputView属性,UITextFieldUIDatePicker

此代码段可能会帮助您...

-(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
{

}
于 2012-08-02T05:52:34.533 回答
1

不,不是。如果您为每个单元格使用不同的重用标识符,则表 wiev 将无法重新排列单元格,因此您将浪费内存。改为使用该tag属性。

如果您找到了该单元格,则必须将文本字段作为属性添加到其中,这是访问它的最简洁方式。

于 2012-08-01T22:48:41.947 回答
0

您知道 Apple 制作的名为DateCell的示例代码吗?

图像0

图像1

图2

代码在这里,你可以运行它:

http://developer.apple.com/library/ios/#samplecode/DateCell/Introduction/Intro.html

于 2012-08-01T23:11:32.093 回答
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.
}

这将为您提供更清洁和更好的包含设计。

于 2012-08-02T00:52:53.400 回答