Leon
Leon

Reputation: 715

iOS cannot set UITextField text property in UITableViewCell

I have a UITableView of 'people' for my iOS app. I have created an 'add' screen to add new people to my persistent store, which works perfectly.

This add view uses a form created with a UITableView and subclassed UITableViewCells, with a UITextField and UILabel, which although works well, I am very new to iOS programming and feel that this may not be the most efficient way.

I am trying to re-use this add view to be my detail, add, and edit view, and I can successfully set a 'Person' entity as a property in the detail view. I have the following code in my viewDidLoad method, where the adding property is set in the prepareForSegue method in the previous ViewController :

if (self.adding)
{
    self.editing = YES;
    self.title   = @"Add Person";
}
else
{
    self.title = self.person.name;

    [self setDetail];
}

My problem is that when I try to pre-populate my detail view's fields (my setDetail method), I am unable to set my UITextField text with the name property from my person entity. Here's the code I'm using to retrieve the UITextField and set it's text property with:

form is the UITableView;

UITableViewCell *nameCell  = [form cellForRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
UITextField     *nameField = (UITextField *)[nameCell viewWithTag:100];
nameField.text = self.person.name;

If I NSLog nameCell it returns (null)

I hope that's enough explanation. Any pointers would help a lot!

Upvotes: 0

Views: 1001

Answers (2)

Puttin
Puttin

Reputation: 1615

I don't really understand your question, but cellForRowAtIndexPath may return nil because

Return Value
An object representing a cell of the table or nil if the cell is not visible or indexPath is out of range.

Official Document: cellForRowAtIndexPath:

Upvotes: 0

nkongara
nkongara

Reputation: 1229

Instead of setting in viewDidLoad:, do it in your table view data source methods. i.e

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     // Deque the nameCell and prepare the cell if its not available.
     UITextField     *nameField = (UITextField *)[nameCell viewWithTag:100];
     nameField.text = self.person.name;
     return nameCell.
}

Upvotes: 1

Related Questions