I have a table populated with a mutable array. The cells are custom, with an UITextView, delegated on the customcellclass, which value I’d like to keep. It gets erased after every table reloadData.
Any idea about how to keep it? The ideas I had, and not able to go for:
- Store the textView value on the same array which populates the table. From where? TextViewDidEndEditing? How to pass the value, and how to know which row was it?
- Maintain an array of textViews apart from the other. Same problem of communicating cell-table and then I have to maintain this array also. Besides, I should take the textView away from the .nib.
…I don’t came up with more know.
Any approach or help will be much appreciated. Thank you!
PS: I did lots of searches, most directing to forms, but this is a mutable array, I’ll have undefined rows on the table.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
GuiCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"GuiCustomCell" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (GuiCustomCell*)view;
}
}
}
if (timesArray || [timesArray count]) {
TakenTime *time = [[TakenTime alloc] init];
time = [timesArray objectAtIndex:indexPath.row];
if ([timeFormat isEqualToString:@"seconds"]) {
cell.detailTextLabel.text = [time stringTimeFormat2];
} else {
cell.detailTextLabel.text = [time stringTimeFormat1];
}
cell.detailTextLabel.font = [UIFont systemFontOfSize:25];
cell.textLabel.font = [UIFont systemFontOfSize:18];
cell.textLabel.textAlignment = UITextAlignmentLeft;
cell.detailTextLabel.textAlignment = UITextAlignmentCenter;
cell.textLabel.text = [NSString stringWithFormat:@"%i.",indexPath.row+1];
}else{
cell.textLabel.text = @" ";
}
[cell setDefaultText:TRUE];
return cell;
}
Set
cell.textLabel.delegatetoself(yourUIViewController), then in thetextFieldDidEndEditing:method, you can retrieve the indexPath corresponding to the cell in which theUITextFieldis located this way:Of course, in your
tableView:cellForRowAtIndexPath:method, you will retrieve the text value stored in[self.mutableDictionaryOfTextValues objectAtIndex:indexPath]and affect it tocell.textLabel.textto restore the previously typed text.Don’t forget to instanciate your
NSMutableDictionary(self.mutableDictionaryOfTextValues = [NSMutableDictionary dictionary];) in yourinitmethod of yourUIViewControlleror some place similar.Note that this will only work if you end the edition of the
UITextFieldbefore scrolling, that is when your UITextField receiveresignFirstResponder(e.g. if you dismiss the keyboard, or the user click in anotherUITextFieldthat will become the new firstResponder), because that’s when thetextFieldDidEndEditing:delegate method is called.If you want to store the text each time the user type a character or modify the text, without waiting for the user to go on another textfield and
textFieldDidEndEditing:is fired, you can use thetextField:shouldChangeCharactersInRange:replacementString:delegate method instead: