开发者

Saving data from custom UITableViewCell

  1. I have an edit screen which is comprised of one huge UITableView.

    1. Each cell in the table is a custom UITableViewCell comprising of a UITextField

    2. I am able to load up the table, scroll around and enter information. However, during scrolling the table cells lose the data entered and I understand why this happens.

    3. I realize I need to save the data as soon as the user finishes entering the data using the textFieldDidEndEditing method.

Question: I want to store the data entered in a custom object that is available in the ViewController - how do I access this from the UITableViewCell? Also, how do I identify the row and position of the table cell so that based on 开发者_JAVA百科the position I can populate a specific attribute in the object

Question: Am I doing this the right way? Or is there a better way to do this?


As you are mentioning only a single text field, we can make do with an NSMutableArray object containing strings for each cell. You will have to fill the array with empty strings and use this info to fill the text field. It will be something like,

textField.text = [textFieldStrings objectAtIndex:indexPath.row];

Since you're doing it programmatically within the view controller, you will have to set the view controller as your text view delegate. You can do

- (void)textFieldDidEndEditing:(UITextField *)textField {
    UITableViewCell *cell = (UITableViewCell*)textField.superview;

    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    ... Since you've the index path, you can map it to an array.

    [textFieldStrings replaceObjectAtIndexPath:indexPath withObject:textField.text];
}

This would be the basic idea. Start with a mutable array of empty strings and modify them every time the text field is updated by replacing the original text with the new text. Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜