UITableView with UITextField - keep the data even after user scrolls

╄→尐↘猪︶ㄣ 提交于 2020-01-02 06:31:23

问题


I have a UITableView and it has bunch of UITableViewCells - These UITableViewCells have multiple UITextFields inside it.

Now every time I scroll up and down - and the UITableViewCells go out of the view - and comes back in whatever text I enter inside the UITextField disappears - What is the best way to make this work?


回答1:


create an NSMutableArray which at each position, holds an NSString object which matches each cell.

when you config + show each cell in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath, set the text of the UITextField to the string in the array at position indexPath.row.

when editing a UITextField, insert/update the NSString object at the current indexPath.row position in the array




回答2:


If you are adding a UITextField in a UITableViewCell, you need to take care of cell's state as the cell gets dequeued while scrolling.

You need to maintain a Dictionary to retain text in UITextField which is present in a UITableViewCell.

Refer my github repo for complete solution of having UITextField / UIButton state in a uitableviewcell.

Code was created in Swift 4 with XCode 9.4



来源:https://stackoverflow.com/questions/4541146/uitableview-with-uitextfield-keep-the-data-even-after-user-scrolls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!