0

I have a table in one of my views, I want to use the table to allow the user to input data. To do this I created a custom UITableViewCell which worked perfectly.

My problem occurred when trying to get the text out of each cell. I have had a look through all the documentation and can't find any solution. Any help would be much appreciated!

The code for this method is:

    Subject *subject = [self.arrayOfSubjects objectAtIndex:i];

    AddSubjectDetailsCell *cell;
    NSIndexPath *indexPath = [[NSIndexPath alloc] initWithIndex:i];
    cell = [tableView cellForRowAtIndexPath:indexPath];

    subject.title = cell.subjectTitleField.text;
    subject.room = cell.subjectRoomField.text;
    subject.teacher = cell.subjectTeacherField.text;

Does anyone have a way of converting a regular cell to a custom one or another way to extract the text from the text field?

Thanks!

Ben Toogood
  • 469
  • 4
  • 9
  • how does your custom cell look like? does it extends from the uitableviewcell or you are just using the uitableviewcell and put custom view in it? – evanwong Jul 11 '13 at 14:10

2 Answers2

1

You are already getting an instance of AddSubjectDetailsCell back, it is just being returned to you typed as the parent type UITableViewCell because that is all the UITableView knows. All you've got to do is cast it to it's specific type like this:

AddSubjectDetailsCell *cell;
NSIndexPath *indexPath = [[NSIndexPath alloc] initWithIndex:i];
cell = (AddSubjectDetailsCell *)[tableView cellForRowAtIndexPath:indexPath];
Timothy Moose
  • 9,895
  • 3
  • 33
  • 44
0

@Timothy's answer might be a solution, but why don't you link model objects to the custom cells cells on instantiation?

Instead of querying the UI in the end you could update your model right in the cell implementation.

I'd definitely add a weak reference to the corresponding Subject for each cell, then update it on cell user actions as you go.

Some kind of design like this, even if not that encapsulated: http://eppz.eu/blog/custom-uitableview-cell/

Geri Borbás
  • 15,810
  • 18
  • 109
  • 172