5

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.

pkamb
  • 33,281
  • 23
  • 160
  • 191
fruitcoder
  • 1,073
  • 8
  • 24

1 Answers1

4

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;
jlehr
  • 15,557
  • 5
  • 43
  • 45
  • Hey jlehr, thanks for the tip but I have called become firstresponder from pretty much everywhere, inuding viewdidload ans didSelectRowatIndexPath, so the cell should be in the responder chain right? – fruitcoder May 30 '13 at 09:10
  • The method you posted is dequeueing the cell from a storyboard. At that point the cell is in memory, but hasn't been added to the table view yet. So no, it's not in the responder chain yet, and therefore the call to `becomeFirstResponder` has no effect. – jlehr May 30 '13 at 14:08
  • but when I call becomeFirstRepsonder in the didSelectRowAtIndexPath, it should be :) – fruitcoder May 30 '13 at 20:50
  • How can I find out when it's been added to the responder chain? – fruitcoder Jun 02 '13 at 10:01
  • 1
    UIKit views are automatically added to the responder chain when they're added to a subviews array, or when they're loaded into the `view` property of a view controller. – jlehr Jun 02 '13 at 20:17