开发者

How to read/store values of all textfield (textfield is in cell of uitableview)?

I have a dynamically created table every time wi开发者_StackOverflow中文版th textfield in cell of tableview. I can read on value at a time from textfield in my subclass of tableviewcell with this:

-  (void)textFieldDidEndEditing:(UITextField *)textField {

NSLog(@"Text :%@ ",textField.text);
 }

But I want to store all values entered in all textfield...how should i approach? I know I can use for loop but im not able to get the conditions (how long it should run) in for loop


Never store data in the view. And in this case the UITableViewCell is your view. When you move the cell off screen it is (hopefully) reused and the text would be gone.

save the data back to your data model in textFieldDidEndEditing:

- (void)textFieldDidEndEditing:(UITextField *)textField {
    //                                          | cell.contentView  |
    UITableViewCell *cell = (UITableViewCell *)[[textField superView] superView];
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];
    MyObject *mydataObject = [self myObjectAtIndexPath:indexPath];
    mydataObject.text = textField.text;
}

there is no way to loop through all (I know about visibleCells) cells in a UITableView. Because there is no cell for item 10 if only cells 1 to 5 are on screen. And if you've scrolled down to cell 10 cell 1 isn't there anymore.


One way to do this is to tag each text field with the index of the cell it is contained in.

You can create an NSMutableArray with the strings for each cell, and on the textFieldDidEndEditing function, pull the tag from the textfield, and place the new string in the appropriate array index.

Make sure to clear the textfields and reset the tags when the cell is reused.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜