简体   繁体   English

UITableViewCell-不要删除和回收

[英]UITableViewCell - don't remove and recycle

I have a UITableView with some cells, containing some UITextField's. 我有一个包含一些UITextField的单元格的UITableView。 It's some very small cells and they do consume a lot of memory. 这是一些很小的单元,它们确实消耗大量内存。

My problem is, that when you scroll, the cell will be unset for memory reasons, which is good - if you have a enormous UITableView. 我的问题是,当您滚动时,由于内存原因,该单元格将未设置,这很好-如果您有一个庞大的UITableView。

My UITableView is only up to 10-15 cells, and it contains, as said, some textfields. 我的UITableView最多只有10-15个单元格,并且如上所述,它包含一些文本字段。 These textfields are written by the user itself. 这些文本字段由用户自己编写。 But when the user scrolls, the values disappear, because the cell is unset. 但是,当用户滚动时,值会消失,因为未设置单元格。

I could make a NSMutableDictionary to store the data, but it will be overkill for this small job. 我可以创建一个NSMutableDictionary来存储数据,但是这对于这项小工作来说是过大的。 Do you have a good hack to fix this issue, so the values don't disappear on scroll? 您是否有很好的技巧来解决此问题,所以这些值不会在滚动显示时消失?

By all means you need to store the information outside of the cells, preferably in an NSArray in your controller or some other structure. 总之,您需要将信息存储在单元外部,最好存储在控制器或某些其他结构的NSArray中。 The other alternative is to disable recycling by either giving each cell a different identifier or creating them preemptively, saving in an array, and returning them instead of dequeuing, but I don't recommend it. 另一种选择是通过给每个单元格一个不同的标识符或抢先创建它们,保存在数组中并返回它们而不是使其出队,来禁用回收,但是我不建议这样做。

Do it the right way. 用正确的方式做。 Use an NSMutableArray as your main data store (one element per row) and put either a custom object or an NSMutableDictionary in each valid element. 使用NSMutableArray作为主要数据存储(每行一个元素),然后在每个有效元素中放入自定义对象或NSMutableDictionary。 It's much simpler to do this in a uniform fashion rather than trying to juggle a bunch of different values. 以统一的方式进行此操作要比尝试兼顾许多不同的值要简单得多。

You can store the cells in an array so they won't be freed when scrolled off the screen. 您可以将单元格存储在数组中,以便在滚动到屏幕之外时不会释放它们。

By adding the cells into an array, their reference counts will increase by 1. 通过将单元格添加到数组中,它们的引用计数将增加1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM