I'm subclassing UITableViewCell so I can design my own cells with gestures, etc.
One of the things I'm trying is to delete the cell/row after swiping.
My gesture recognizers are all setup and working nicely, however I'm unsure on how to tell the UITableView to delete this object. How can I reference the parent tableView, tell it which row to delete from within the UITableViewCell subclass?
Thanks
You could define a delegate protocol in your UITableViewCell subclass, add a method like cell:didSwipeRightToLeftGesture, and have your cell call its delegate when that happens. Then when your controller receives that delegate message, delete the row from the data source and update the table view.
You could also use UITableView's built-in swipe-to-delete as well.
Related
In my setup, I have 3 tableViews (nested) - tableView inside a tableView cell inside a tableVIew cell.
My goal is to find out touch events on the “mid-level” tableView. When a mid-level tableView is touched, I would like to extend its and its parent tableView height to predetermined values.
I created a custom UITableView with a second tag property (“secondTag”) and inside the tableView methods, I cast tableView to CustomTableView. So, inside tableView didSelectRowAt I'll had two indexes - IndexPath and secondTag. The first one was theIndexPath of the mid-level tableView and the second one was the custom UITableView tag that used for the indexing of the “parent” tableView.
But despite setting the cell identifier properly (inside the storyboard) I got the following error: "unable to dequeue a cell with identifier middleCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard".
One more note: inside tableView didSelectRowAt I used reloadData() on both the mid-level tableView cell and its parent tableView cell, since I needed both of them (sub/parent tableView cells) to change their height after a touch has occurred.
I think the double reloading may be the cause of my problems but I must reload both the parent and its nested “mid-level” tableView since I need both their heights to change after a touch was made.
*This question comes in to continue the following one: Need help adding new stored property to UITableView
When cells come in and out of device's screen, I want my viewController to know exactly what came and what went out. Is there a way to do this?
There isn't a delegate method just for "visible cells". There isn't anything called when a cell leaves the screen. There really isn't anything when a cell becomes visible.
There is the cellForRowAtIndexPath data source method. This is called when a cell is needed.
There is the willDisplayRowAtIndexPath delegate method. This is called when a cell will be displayed.
There is the didEndDisplayingCell delegate method. This is called when a cell is removed from the table view.
There is the indexPathsForVisibleRows method on UITableView. This lets you know what rows are currently in view.
There is the prepareForReuse method on UITableViewCell. This lets a cell reset itself to be reused for another row.
Better describe what you are trying to accomplish in order to get a more specific answer.
How to add UIPickerView in UITableView in didSelectRow method ,
Based on picker selection user select different options in each cell
First, when you want to ask a question, you should add some code pieces that you tried. In this way, people can help you using your work.
ANYWAY, I suggest you two ways:
Create a custom UITableViewCell class and in setSelected: method create a UIPickerView and add it to window. Set its delegate and dataSource. Use delegate methods to get data and use dataSource methods to set data to UIPickerView.
In UITableView's tableView:didSelectRowAtIndexPath: method and implement UIPickerView delegate and dataSource methods same as in first option. Update cells' data using these methods.
How can I reload a TableView using delegation from within my CustomTableViewCell class?
Currently I'm doing it by calling this:
NSNotificationCenter.defaultCenter().postNotificationName(UIContentSizeCategoryDidChangeNotification, object: nil)
But I would like to do it through delegation so I can have more control over the animation and reload only that specific TableViewCell instead of the whole thing.
Any help is greatly appreciated :)
As #Paulw11 suggests in his comment, you can implement a delegate pattern to call back from your custom UITableViewCell to the controller. Be careful not to create any circular references (use weak properties!)
You could also count on the fact that the UITableViewCell is child of the UITableView. That is, if you need to get to the UITableView (vs your controller) you can walk up the superview chain until you find it.
I have tried to create a custom cell that would display a list of items. To achieve this I tried to create a custom cell with UITableView inside it with scroll disabled.
The problem I have with this is when i try to apply changes to the cells in the inner UITableView data just does not get updated and the cell stays as it was. I have tried calling [tableView reloadData], [cell setNeedsDispaly], [cell setNeedsLayout] to no avail.
It seems like the data that has been applied when the cell was initialised persists through any attempts to change it. Though, when I create a breakpoint in cellForRowAtIndexPath: the data does get updated but is not rendered.(e.g. text property of UILabel has new value, but text is old on the screen.)
You need to nil the cell and then reload the table view. For some reason iOS caches table view data and stuff doesn't get updated correctly.
You could try overriding UITableViewCell's prepareForReuse(): in that method you can set all the cell's outlets/properties to nil. You would do this for the cells in the main cell's table view. I am assuming those are custom cells as well and that you have a custom UITableViewCell subclass for them.
Refer [I have two views on one cell, when I click on a cell it will be hidden and one edit form will be expanded on that. How to resolve that? ..check in accepted answer methods ...
[tableView reloadRowsAtIndexPaths:ll withRowAnimation:UITableViewRowAnimationAutomatic] in didSelectRowAtIndexPath