简体   繁体   中英

Stop cellForRowAtIndexPath from being called

I have a table view in my app where the cells contain only one textview per cell. These textviews are populated with text stored in the 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 . However, the text of the textviews in the cells are set in 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 .

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. So it was working but only loaded the text for the first cell's textView because I only let cellForRowAtIndexPath do that once.

My question is: How can I let cellForRowAtIndexPath change the textView's texts of all the cells only when viewDidLoad is called as well? Or can I set the textView's text for each cell from the viewDidLoad method? 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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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