简体   繁体   English

阻止cellForRowAtIndexPath被调用

[英]Stop cellForRowAtIndexPath from being called

I have a table view in my app where the cells contain only one textview per cell. 我的应用程序中有一个表格视图,其中单元格每个单元格仅包含一个textview。 These textviews are populated with text stored in the NSUserDefaults. 这些文本视图使用存储在NSUserDefaults中的文本填充。 The user can change the text and than it is saved again. 用户可以更改文本,然后再次保存。 The text array(the text which is used to populate the textviews) from NSUserDefaults is loaded at viewDidLoad . 来自NSUserDefaults的文本数组(用于填充文本视图的文本)在viewDidLoad处加载。 However, the text of the textviews in the cells are set in cellForRowAtIndexPath . 但是,单元格中文本视图的文本是在cellForRowAtIndexPath中设置的。 The problem is that cellForRowAtIndexPath is called when ever the user scrolls the tableview so if he/she is editing one of the textViews and scrolls while still typing, everything disappears because cellForRowAtIndexPath is called and the textViews text's are set from the saved data which was loaded on viewDidLoad . 问题是,每当用户滚动表格视图时都会调用cellForRowAtIndexPath ,因此,如果他/她正在编辑textViews之一并在仍然键入时滚动,则所有内容都会消失,因为调用cellForRowAtIndexPath并从已加载的已保存数据中设置了textViews文本在viewDidLoad

So..I figured maybe letting cellForRowAtIndexPath change the text of textviews only once(at app start) will solve the problem but as it turns out cellForRowAtIndexPath is called individually for every cell. 所以..我想也许让cellForRowAtIndexPath仅更改一次文本视图的文本(在应用程序启动时)就可以解决问题,但是事实证明,每个单元格都单独调用cellForRowAtIndexPath So it was working but only loaded the text for the first cell's textView because I only let cellForRowAtIndexPath do that once. 这样就可以了,但是只加载了第一个单元格的textView的文本,因为我只让cellForRowAtIndexPath做一次。

My question is: How can I let cellForRowAtIndexPath change the textView's texts of all the cells only when viewDidLoad is called as well? 我的问题是:仅在调用viewDidLoad时,如何才能让cellForRowAtIndexPath更改所有单元格的textView文本? Or can I set the textView's text for each cell from the viewDidLoad method? 还是可以通过viewDidLoad方法为每个单元格设置textView的文本? As that would clearly solve the problem. 这样可以清楚地解决问题。

Whenever the user changes text in the textview in one of your cells - save that text immediately to your textArray. 每当用户在您的一个单元格中的textview中更改文本时-立即将该文本保存到您的textArray中。 Now if user scrolls away, and then returns to that cell - the cell will contain the updated text. 现在,如果用户滚动离开,然后返回到该单元格-该单元格将包含更新的文本。 Then when the user finishes editing the text in this screen, save all changes to NSUserDefaults (for example, you can save it in viewWillDisappear). 然后,当用户在此屏幕上完成文本的编辑时,将所有更改保存到NSUserDefaults(例如,您可以将其保存在viewWillDisappear中)。

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

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