Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField inside UITableViewCell won't become first responder

I use a UITableViewController to display the details of KoreanFood. The first cell is a custom UITableViewCell (OverviewCell) with an Image and two UITextFields, which I created and layout in Storyboard (AutoLayout).

I subclassed UITableviewCell like this:

// OverviewCell.h

@interface OverviewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UITextField *englishTitleTF;
@property (strong, nonatomic) IBOutlet UITextField *koreanTitleTF;
@property (strong, nonatomic) IBOutlet UIImageView *myImageView;
@property (strong, nonatomic) UIImage *thumbnail;

My textfields in Storyboard are set to enabled/UserInteractionenabled and the delegate is my TVC. When I create the cells I also do this in code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == GENERAL_SECTION) {
        static NSString *overviewCellID = @"overviewCell";
        OverviewCell *overviewCell = [tableView dequeueReusableCellWithIdentifier:overviewCellID forIndexPath:indexPath];
        
        overviewCell.englishTitleTF.text = self.generalInfo.titleEnglish;
        overviewCell.koreanTitleTF.text = self.generalInfo.titleKorean;
        overviewCell.englishTitleTF.enabled = YES;
        overviewCell.koreanTitleTF.enabled = NO;
        //BOOL test = [overviewCell.englishTitleTF becomeFirstResponder];
        overviewCell.koreanTitleTF.delegate = self;
        overviewCell.englishTitleTF.delegate = self;
       
        UIImageView *imageView = [[UIImageView alloc] initWithImage:self.generalInfo.thumbnail];
        overviewCell.myImageView = imageView;
        overviewCell.myImageView.frame = CGRectMake(25, 25, 95, 95);
        [overviewCell addSubview:overviewCell.myImageView];

        return overviewCell;
    }

The comment with the BOOL is NO, and I just don't know why... As I set the text and it's displayed correctly, I know the Outlets are set and the Cell isn't nil (I checked that).

Why does this not become first responder?

I also tried some suggestions inside the OverviewCell subclass like the hit test or implementing the setSelected: / setEditing: methods. But a becomeFirstResponder to the textField here doesn't change anything as well.

like image 715
fruitcoder Avatar asked Oct 04 '22 00:10

fruitcoder


1 Answers

A view can't become first responder until it's been added to the responder chain, which happens automatically when the view gets added as a subview of a view that's in a window in the application object's windows list. In other words, your cell hasn't been added to the table view yet, so it's not connected to anything, and hence can't become first responder.

Try sending becomeFirstResponder from a method that gets called after the table view has finished loading its cells. And of course, don't do this:

overviewCell.koreanTitleTF.enabled = NO;
like image 93
jlehr Avatar answered Oct 13 '22 11:10

jlehr