Is it possible to change attribute of individual UITableViewCell? - ios

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.

Related

UITableview footer getting refreshed while scroll down?

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.

UITableView trigger when cell is reused

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.

Change another cell in dynamic prototype UITableView when a switch in one cell change

I used to use static UITableView but the table is too long and overflow the memory.
I switched a dynamic prototype UITableView with 1 type of cell, which has an UISwitch in it.
One of the cell, when turning on the switch, will turn off the switch of another cell. These cells have fixed index.
The IBAction method is in my UITableViewCell subclass and I don't want to add the UITableView as a property in my UITableViewCell.
How do I achieve the above effect?
I'm planing to use an id or similar to distinguish between the cells as each cell's switch has different effects, that doesn't solve the above requirement.
Thanks,
I would add a block property to your cell which you can use to notify your controller of changes in the switch. See my answer below to a question on this:
How can I get index path of cell on switch change event in section based table view
All your logic can now be implemented in the view controller.
You are best to create a data model in the view controller which the cells simply provide views and controls onto. When you flick one switch and the block fires, update the data model and simply reload the table. Any cells affected will show the new data model positions for their switches. Avoid using one cell to adjust another. Just update the model and reload the cells.

iOS Maintaing selected button in Tableview when scrolling

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.

edit the text of a cell?

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.

Resources