Nick
Nick

Reputation: 19664

How can I get reference to UITextField from UITableViewCell?

I have create a static UITableView using the storyboard. Each row in the table view contains a textfield and I have given each row an identifier . One of the textfields is used to input a date. I would like show the UIDatePicker when this textfield is selected. I am trying to set the text field's inputview to a UIDatePicker to accomplish this. I am struggling getting ahold of the UITextField on this particular UITableViewCell. So here is the direction I am going:

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

    }


}

Is this the right approach? If so how do I find the textfield on this cell?

Thanks!

Upvotes: 1

Views: 1357

Answers (4)

Shanmugaraja G
Shanmugaraja G

Reputation: 2778

You can use the indexPath for setting the inputView property of the UITextField as UIDatePicker,

This code snippet may help you...

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

}

Upvotes: 2

Feel Physics
Feel Physics

Reputation: 2783

Do you know the sample code made by Apple named DateCell ?

image0

image1

image2

The code is here and you can just run it:

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

Upvotes: 0

mamackenzie
mamackenzie

Reputation: 1166

Have you considered the possibility that UITableView is a poor choice for this particular problem?

Think about it - each of these "cells" seems to look and behave differently and fulfill a different purpose, and there are a fixed number of them that have no need for dynamic data binding.

If I were you, I would just make custom views for each of the input fields and plop the whole thing in a UIScrollView.

Even if you are steadfast in this approach, there is a problem. UITableViewCells DON'T have UITextField properties, and thus you won't be able to access them. Just make a class like this:

@interface DatePickerView : UIView <UITextFieldDelegate>

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

@end

Now all of the logic is contained within the DatePickerView class. You can implement the UITextFieldDelegate methods to handle input, without worrying about which text field you selected. In the implementation of DatePickerView, you can do:

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

This will give you a much cleaner and better contained design.

Upvotes: 0

user529758
user529758

Reputation:

No, it isn't. If you use a different reuse identfier for each cell, the table wiev won't be able to requeue the cells, so you'll waste memory. Use the tag property instead.

If you have found the cell, you have to add the text field as a property to it, that's the cleanest way to access it.

Upvotes: 1

Related Questions