I’ve been really really baffled by this for hours now, so I figured it’s time to seek help!
I am trying to access the text value in a UITextField that is embedded in a UITableViewCell subclass (QuestionCell).
The problem is whenever I try to access the cell with the cellForRowAtIndexPath method, it will return (NULL).
QuestionCell *qc = (QuestionCell*)[questionsTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
NSLog(@"qc %@",qc); // Gives "qc (NULL)" if there's text in the custom cell
This only happens if there’s text entered in the UITextField in the custom cell. If there’s no text, the cell will be returned as expected.
I am using XCode4.2 with storyboard, with a custom style and class for the UITableViewCell
Here’s my QuestionCell.h
#import <UIKit/UIKit.h>
@interface QuestionCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *correctionLabel;
@property (weak, nonatomic) IBOutlet UIImageView *correctionMark;
@property (weak, nonatomic) IBOutlet UILabel *pronounLabel;
@property (weak, nonatomic) IBOutlet UITextField *answerInput;
@end
And the cells are created with the usual delegate method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0)
{
QuestionCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:@"QuestionCell"];
NSString *pronoun = nil;
switch (indexPath.row) {
case 0:
pronoun = @"je";
break;
case 1:
pronoun = @"tu";
break;
case 2:
pronoun = @"il";
break;
case 3:
pronoun = @"nous";
break;
case 4:
pronoun = @"vous";
break;
case 5:
pronoun = @"ils";
break;
default:
break;
}
cell.pronounLabel.text = pronoun;
return cell;
}
if (indexPath.section == 1)
{
ActionCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:@"ActionCell"];
return cell;
}
return nil;
}
There’s nothing wrong with the above code. I created a test project, inserted this
QuestionCell(complete withUITextFieldandUILabelobjects) and yourtableView:cellForRowAtIndexPath, and everything is fine.The
UITableViewmethodcellForRowAtIndexPathonly returnsnilif the cell has scrolled off the screen (and that’s expected behavior). But with these six cells present, I iterated through the table’s `cellForRowAtIndexPath and everything was fine, regardless if I’ve entered text or not.Judging from your subsequent comments, it appears that your cell has scrolled off the screen. Yep, that will definitely cause
cellForRowAtIndexPathto returnnil!