image is reverting to original when scrolling uitableview - iphone

I have a button inside each cell. When it's pressed, the image is changed (basically a checkbox) to denote a selection. When you scroll to the bottom ... then scroll back up to the top. The image is reverted to the original image.
This question is pretty similar to this:
Preserve Cell Image After Scrolling UITableView
And others. But, I can't seem to find a good answer. I understand that's it's reverting back to how the uitableview is setup when the cell goes off the screen. But, how do I save the changed image to the uitableview so when it scrolls it doesn't revert to the original?
Thanks in advance! =)

It's changing back because cells are reused. When your cell is going off the screen it is taken out of the view and put back into the reuse pool. Then you're getting it out of the queue again in cellForRowAtIndexPath and setting it back up as the default.
The question you linked to is exactly what you should follow. You should store the state of your cell in your view controller and then when you set it up again in cellForRowAtIndexPath you should load that state and set up the cell appropriately.
One simple way for your method would be to have an NSArray which you set up to be the same size as the number of rows in your table and then in that just store an NSNumber for each row which contains a boolean value on or off for your selection state. When the user toggles, toggle the value in the array and then in cellForRowAtIndexPath read that value and set it up appropriately.

I'm assuming the checkbox in your table view cell is changing state to a selected state because a user selected it. You shouldn't use UI elements to maintain the state of your app. That is, when the user taps the checkbox, you should use that event to somehow reflect that state change in a data object in your app. Then, when that cell needs to be displayed again, you configure it with the state you previously saved. This allows for things like cell reuse, and view unloading and is all-around a good habit.

Related

How can I empty my cell data inside a table view by pressing a button outside the table view?

I have a table view in my app with 4 different types of cells in it. I would like to be able to press a button in the nav bar (a clear button) and be able to empty the data in my cells. Some of my cells have segmented controls in them and I would like to be able to set them to zero after this button is tapped. How can I accomplish this, even for cells that are not in view but still need to be emptied?
Stop thinking about your cells and think about your data model. When the user presses the button, you reset the data. You then reload the table view, and your changes are picked up by the visible cells and all future cells, because you have cleverly written your cellForRowAt to make the cell reflect the data.
Basically, what I've just said is more or less the golden rule of table views. You never operate directly on a cell; you operate on the data and reload the table, so that any cell that needs the data can retrieve it on demand.

iphone tableview scrolling issue

I have a tableview with imageviews and textviews in each cell. When my view is loaded one of the textviews is invisible. But when I start scrolling the textview appears, so that my view looks as it is supposed to look. Does anyone know why this is happening?
P.S. I have read about reusing cells when scrolling so I have been very careful to construct my cells correctly.
Without code, no one can give you a exact answer. But for a guess....
Is your data is getting populated after your table call cellForRowAtIndexPath? When the cell populates the first time, no data, so the cell is in it's unformatted state. By the time you can interact with the table and scroll it off and on screen (which calls cellForRowAtIndexPath again), the data has been populated and so the cell looks as expected.
You can test this by putting a breakpoint in your cellForRowAtIndexPath method and check to see if your data objects are initialized or still set to nil.

iphone sdk stay the selection colour of cell in UItableview?

How to get the selection cell still in the selected state in uitableview. Actually I am pushing a view from a tableview when came back then the selection cell is deseleted. I want to be show the previous selection cell when came back also how to do that.
In didselectrowatindexpath use the method selectRowAtIndexPath:animated:scrollPosition: and the row stays selected.
when you are going and coming back from anotherview make sure that save the selectedCell and then in viewwillappear method reloaddata.In cellforindexpath write the code of selection style uitableviewcellselectionstyleblue.
first, you can store the selected indexPath in user default in the didSelectRowAtIndexPath method.(maybe you can store a indexPath that is bigger than your table view's row count to indicate initialization stage, which means that no row has ever been selected)
then, you can load the stored indexPath in the viewDidLoad method using
tableView selectRowAtIndexPath:<#(NSIndexPath *)#> animated:<#(BOOL)#> scrollPosition:<#(UITableViewScrollPosition)#>
meanwhile, you can do some check (like mentioned above or something else) to check if there's nothing selected yet.(saving a bool in user defaults works, too)

Woes with the selected button state in custom UITableViewCells

I have a custom UITableViewCell with two buttons in it, mycell.m/mycell.h/mycell.xib. In the XIB I have given the buttons a sample label, and I change that text in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *).
Now, when I click the buttons, while I'm holding my finger on it, it will show the sample text instead of the actual text. And should I miss the button and touch inside the custom UITableViewCell, both buttons change text to the sample text, and they don't redisplay the text I set in cellForRowAtIndexPath until I scroll the cell off-screen and back again.
Any clues to what I might be doing wrong? I haven't found any way to set a selected text.
Cheers
Nik
Well, trying again...
I set up a mini project and recreated the problem. After puzzling over why the text I set wasn't being used, I finally realized that you can set a different title for every control state (setTitle:forState:). Normally, if you just set titleLabel.text, then you're setting it for UIControlStateNormal, which is then used by default for all other states. However, IB apparently sets the sample title you specify for all the states separately, not just for normal state. So in your code, you're indirectly doing a setTitle:forState:UIControlStateNormal, but the titles set by IB for all the other states are remaining unchanged. So when you hold down the button (UIControlStateSelect) or you hit the cell (UIControlStateHighlight), then the sample label is displayed. SO...either remove the sample label in IBuilder (the easy path), OR set the label for all the relevant states.
Old incorrect answer:
cellForRowAtIndexPath is only called when the tableView needs to display a cell (e.g. when scrolling back onto the screen.) It's not called anytime a cell is on the screen. Sounds like you've got another routine (e.g. buttonPressed) that's set up as an IBAction from the buttons.

Some cell in table view should be checkmarked?

I am making app where a list of countries to be shown. When the first time app runs the user selects the no. of countries. I stored that list in nsuser defaults. I want to checkmark those countries in the TableView when the app runs again for second time.
One more problem is when I scroll through the TableView and again come to the previous position, the checkmark does not show. Why this happens??
In -tableView:cellForRowAtIndexPath: you should set wether or not a checkmark should be shown based on your internal model (which in your case comes from NSUserDefaults). In the tableView:didSelectRowAtIndexPath:, do not simply make the checkmark visible in the cell, but store in in your internal model as well. This way when a cell is being shown again, it will show up correctly.
This happens because the table Cells are reused so when you Scroll up they are cleaned and reused by the below data and when you go up again all the thing is happened again.
Regarding Checking again on second time.
Just store the index of the row in NSUserDefault and put the condition in cellForRowAtIndexPath that when the index is matched keep it Checked.
Hope this helps..
hAPPY cODING...