【问题标题】:How can I get reference to UITextField from UITableViewCell?如何从 UITableViewCell 获取对 UITextField 的引用?
【发布时间】:2012-08-01 22:42:06
【问题描述】:

我已经使用情节提要创建了一个静态 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

    }


}

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

谢谢!

【问题讨论】:

    标签: iphone objective-c cocoa-touch


    【解决方案1】:

    您可以使用indexPathUITextFieldinputView 属性设置为UIDatePicker

    这段代码 sn-p 可以帮助你...

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

    【讨论】:

    • 这很好,但是如何在 dateChanged 方法中获取对 textField 的引用以将其文本设置为选定的日期?
    【解决方案2】:

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

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

    【讨论】:

    • 谢谢。我在 IB(故事板)中创建了单元格。那么文本字段不应该已经是单元格的属性了吗?
    • 如果您没有在代码中声明它(或者如果它不存在),那么不要。 (这就是为什么我通常不鼓励新手使用 IB - 这令人困惑。)
    • 这令人困惑。我试试看
    【解决方案3】:

    您是否考虑过 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.
    }
    

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

    【讨论】:

      【解决方案4】:

      你知道苹果制作的名为DateCell的示例代码吗?

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-09
        • 1970-01-01
        • 1970-01-01
        • 2013-10-24
        • 1970-01-01
        • 2014-07-27
        • 2012-03-23
        • 2014-09-25
        相关资源
        最近更新 更多