I had added a UITableView footer In Section and made a custom view for it. I have a checkbox in the custom view. The problem is that I had checked the checkbox and when I scroll down the footerview gets refreshed and the checkbox will be unchecked. Is there a way that I can stop the unchecking the checkbox while i scroll the table?
Update your datasource with the selection and use the same data when scroll back to show the previous status.
You need to handle it, Like Cell dequeued same way header and footer also dequeued you need to user reuse Identifier check the below answer, you will get the hint.
The below link will not give an exact answer but you can get a hint.
How to Implement Custom Table View Section Headers and Footers with Storyboard
There is a RULE for UITableView/UICollectionView. If you are displaying some data into these controls then YOU MUST KEEP your data/state into your Model.
The reason of this rule is these controls take only one cell and REUSE that cell. So you have to maintain your data or state of controls into your model i.e. State of checkbox in your case.
Related
Is it possible to make changes to a particular UITableViewCell? For example, change the text in one cell of a table view with a button click?
It's possible, but you should not do that.
As you said in your comment, you can ask the table view for a cell and then make changes to that cell, but don't do that.
You should do what HHumorous said, and change your data model, then tell the table view to reload the affected cell.
If you simply change the appearance of the cell, then when the user scrolls that cell off-screen and then back on-screen the changes will be lost.
I'm having some issues with reusable cells in a UITableView. I have several types of cells, that I declare in the constructor.
My issue is that I have one particular type of cell that contains a UITextView and I have an issue when I scroll the table, the text within is lost. I need to save this text to the models that accompany the cells and then put the text back when the cell is used again.
How do I know that the cell is being moved away from? I have other types of cells, so I need a way to invoke some code to do the saving part on the scroll of the UITableView.
I hope that makes sense, if more is required, let me know.
Thanks.
Just save the text once it is changed to the model, check if any text is present and use that in tableView(_:cellForRowAt:)
For more help you will have to show us your code.
You can inherit UITextViewDelegate and in the textViewDidEndEditing(_:) check if the text view is edited, then you will be able to store the text in a variable or somewhere else and restore it whenever you are about to show that cell again.
If there is more than one text view, you might want to set an accessibility identifier for each kind, so you will find out which one did end editing.
I have another question open where I'm trying to figure out how to reload the collectionView without auto-scrolling. I was also realizing there are a lot of other situations where I will need to change things in the collection view. Also I have some items that I will want to change the .alpha on and change the text of. Is there a way to do all of this in Swift? For example (to be specific) if I have a collection view with a view in each cell and that view has a textField in it, can I change the alpha and text, (change alpha with animation even) without reloading entire table?
Look at the documentation for UICollectionView. There are several "reload" methods:
reloadData()
reloadSections(_:)
reloadItems(at:)
If you just want to reload a single item, update your data source's data and then call reloadItems(at:) passing in the index path for the item.
Another option, if a cell is currently visible, is to use the cellForItem(at:) method to get a reference to an existing cell. Then you can directly access UI components of the cell as needed. You should also update your data model as needed so if the user scrolls and comes back, the cell will be rendered properly.
Most appropriate where you can update your custom view of a particular UIcollectionViewcell is reloadItemsAtIndexPaths.
You would be handling a particular item than whole collectionview with reloadData.
You can handle it via notifications or some call backs in your code where you can make decision when to update which cell.
Hope it will help you.
I have a UItableView with custom cell. There are multiple sections, and in those sections multiple rows. In each cell, I have a segment button for "Yes" and "No" selections. I would like to maintain the selections for the segment button when I scroll up and down the table. Can anyone please help. I have looked and did not find anything that helped me. Thank you!
You have multiple ways to do this.
The first one, which I think is the best one, is to use the selected state for the uitableviewcell.
With this method, the table view it self will set the cell's state and according to that state will call didselect or diddeselect on its delegate. Since all is managed by the table view, you don't need additional logic on your cellForRow method, and don't even need to worry about cell reuse.
Another way is to always set the state of your cell's buttons in cellForRow. For this, you need to keep a reference to all the selected rows, and to be able to manage reused cells, you need to make sure its state is always set on cellForRow.
I prefer the first one, but whatever suits you better.
I have a UITableView, and want to let users edit the text of a cell. I want to popup a new UIView to let them enter some text in textfield, then dismiss the view and replace the cell's label text. Is it possible?
This is kind of an advanced move, Vikas. The programming of and user interaction with table view cells is tricky. The reason is that the UI code is a bit tricky because of the scrollable view, and you have to be prepared for the table cells to move or change if the table data is reloaded.
Is there another way you can achieve the result you're looking for? Could you design your table view so that when a cell is selected, you push a new view onto the navigation stack, and have UI components on that view that enable the user to edit the value?
This is the way that Apple does the Settings on iOS. When you select a row, you are shown a new view that has appropriate editing or selection controls.
You should use the textLabel property.