Can't update text of UITextView in a custom UITableViewCell - ios

I've been running into an issue that seems to keep coming up on the web, but have been unable to resolve the issue... I'm trying to set the text of a UITextView that's inside of a custom UITableViewCell in a load function that's called on the cell when it's contents are being rendered in its table view.
These are my current settings on my textview that's inside my custom tableviewcell. Note that UserInteraction is NOT enabled, but Editable and Selectable ARE.
This is the controller for my custom UITableViewCell, which contains the function 'loadCell'
This is the cellForRowAtIndexPath that puts the custom tableViewCell into the tableView, and it calls the loadCell() function when it's initialized.
This is the result, with the textView obviously not being updated correctly.
Another note I'd like to add is that if I update the textView during the awakeFromNib function in the custom UITableViewCell controller, it successfully is able to change the text in the textField.
If anyone can help me out, that would be super duper awesome, as I'd love to be able to use textViews instead of labels :) Thanks!

So, it turns out that the problem was in the cellForRowAtIndexPath function, in which I was attempting to dequeueReusableCell twice (once at the functions start, and once if the index was 0). I'm not entirely sure why this is causing a problem, though I can make an assumption that it was attempting to dequeue the same cell twice, and was causing problems.
Either way, getting rid of
let cell = tableView.dequeueReusableCellWithIdentifier("MenuItemDescriptionCell")
did the trick.
Hope this helps someone in the future! Happy coding :)

Related

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.

How can I Create a Tableview Under Custom Tableview cell

I am just thinking. Suppose, i have a tableview which have custom cell. It's simple. But My idea is that, when i click a tableview cell then another tableview is appear under that tableview cell, and again i click that cell then that sub tableview disappear. Similarly when i click second cell than work same. Is it possible? Please Provide me any idea or reference.
This is entirely possible, you're talking about Expandable cells.
My example here
The general idea is that your custom cell has a tableview at the bottom of the cell, and what you do is just change the cell height to display said tableview, on tap.
It's not easy, I'm not gonna lie it took us a while to do it, but we managed, and I'm telling you, it's very possible.
You can find a lot of help using the Expandable Cell keywords.
Note that you're gonna find yourself handling a lot :
What to do when the expanding cells is shown off screen?
What to do when you're expanding the first/last cells ?
What to do when expanding another cell ?
What to do when scrolling inside that cell (a scrollview inside a scrollview !)
There are many cases where it'll work, but won't work fine, and there is gonna be a lot of fine tuning. Specially in our case where we have rounded corners, but only when the cell is expanded, and not in cases where it's the last or first cell (next to section header).
They look cool and make you feel proud, but don't say to your PM it'll be done in a week, because it's a pain to build.
If you want to show additional cell information, you can add more cells after the cell indexpath you have clicked.
Create a custom table view cell classCustomTableViewCell by subclassingUITableViewCell class. And system will generate CustomTableViewCell.h, CustomTableViewCell.m, CustomTableViewCell.xib files for you.
Add protocols UITableViewDataSource and UITableViewDelegate in your CustomTableViewCell.h and implement the required methods in CustomTableViewCell.m files
Add a method for setting datasource and use the datasource for updating the table.
NOTE:
Handle table-dequeue mechanism properly, otherwise you will end up
with weird issues that may take time to investigate and resolve.
If you use this custom cell for all the cells in your parent table then the gestures will only listened by the child table. So plan for that too.
Please visit my blog for the sample code. https://myioslearnings.blogspot.in/2017/03/nested-table-view-in-ios-objective-c.html

Name Label overlapping each time it is loaded

Our app has a social feed with a name label similar to Facebook with a FeedController and FeedCell class. Each time we scroll back to a previously loaded cell, it reloads, so the name label becomes darker, the text becomes bolder and starts to overlap. How do we fix this? We tried the prepareToReuse method and setting the cell's label to nil, but that did not help. Any help would be greatly appreciated!
It appears that you're regenerating the label and adding it to your cell's content view every time that the label is reused.
Inside your FeedCell class, make sure to only initiate your interface elements in your initializer. From there, you can fill your cell with data in the cellForRowAtIndexPath: method of your UITableViewDataSource.
Inside cellForRowAtIndexPath:, make sure to not add any views to the cell as this could contribute to the issue you're describing. As a rule of thumb, I make sure to initialize the views in the cell subclass's initializer, and only manipulate properties of these views (like hidden, alpha, etc) when working with the cells.
Moving forward, you can use prepareForReuse to clear any data from your cell that may be leftover from the previous time the cell was used. In many cases though, this isn't necessary as the data will be changed in cellForRowAtIndexPath:.

How do I get an event message from UITextField in a custom table cell to the table cell?

I'm sure this is a very noddy question, but I just can't get anything useful from the manuals.
I've a UITextField embedded in a custom UITableViewCell. If I use a delegate for the UITextView - it just gives me the UITextView.
If I link the event to the ParerentViewController, it just gives me the ID of the UITextView.
What I want to do is handle the event at the UITableViewCell level. Can I get the events from this text control to feed to the parent, and handle them at the parent level.
I don't seem to even be able to find the parent cell from the UITextField that the handler gives me. How do I find out which cell within the table the UITextField is in when I'm trying to process the events.
I have to be missing something obvious, as I've done this in earlier (pre storyboard) versions, and I'm sure it wasn't this difficult.
Answered a similar question the other day. See the accepted answer.
How can I get index path of cell on switch change event in section based table view
This will let you handle the text changes in the cell and pass them on to the table view. In your case its a UITextView and not a UISwitch.
Add properties to the cell for anything you will need inside the block code when it is called.
Create a subclass of UITableViewCell and implement the textField delegates inside the subclass. The code is lengthy but if you want I can post it when I get time.

Add an action to a custom UITableViewCell

I am building a simple app with a table view filled with custom view cells and using a storyboard. I want to add a actions on the cell each time the user tap it.
So far, I tried to create an IBOutle to link my cell to my tableViewController and add the action manually in the code but each time I try to do it I get an error message saying "Illegal Configuration - cannot have a prototype object as its destination".
The only quick fix I found is to add a UIButton with a transparent background and no title which fills in the whole cell and attached the action to it.
Is there any more elegant way to do it?
Not only is there a more "elegant" solution, but there is also a correct way to do this.You should be using the didSelectRowAtIndexPath method for your cells.
You should read what the Apple Docs have to say
And also, here is a tutorial on how to use row selection in your tableView.
The elegant solution is to use your table view's delegate method tableView:didSelectRowAtIndexPath: and put all your action code there.

Resources