How to notice if a UITableViewCell has left the visible area? - ios

I'm stuck with the problem that I want to know in my UITableView if a specific UITableViewCell, let's say the first, is still visible or already off the visible area.
I would also be ok to know if the cell of interest is now beeing reused at an other indexPath of the table.
One of my - later and frustrated approaches - was to have a thread that knows the first cell object and frequently pings it to check if a value I did set in the cell changed. Obviously a not so good solution.
Andy ideas how to do this right?

Remember that UITableView is UIScrollView subclass and its delegate also confirms to UIScrollViewDelegate protocol as well.
So in your table delegate you can implement scrollViewDidScroll: method and check contentOffset - if it's more then first cell height then first cell is not visible. You can also get the array off all currently visible cells using -visibleCells method, but I think knowing contentOffset should be enough.

Related

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

In UITableView, what's the delegate for "visibleCells"?

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.

Check if a UITableViewCell is selected, and the cell went off screen

I have a custom UITableViewCell that dequeueReusableCells. I have an int called selectedRow which gets the selected rows number in the method of didSelectRowAtIndexPath. I then pass selectedRow to an int called rowNumber which is in the class of my customCell.
In customCell.m, I have the method prepareForReuse. In that I made an NSLog of rowNumber.
What I want to do is: if a row is selected and that row went off screen, then perform some code. I would probably have to use prepareForReuse, but I don't know what to do in it.
I know it's a bit complicated, but if you have any questions, then I'd be happy to answer
Actually, you don't need to call prepareForReuse directly as it would be called automatically:
this method is invoked just before the object is returned from the
UITableView method dequeueReusableCellWithIdentifier:.
and as you don't know what to do in it, note:
For performance reasons, you should only reset attributes of the cell
that are not related to content, for example, alpha, editing, and
selection state
UITableViewCell Class Reference
You can use - (void)tableView:tableView didEndDisplayingCell:cell forRowAtIndexPath:indexPath; in UITableViewDelegate to know which cell is scrolled off screen.
However, this method is iOS6+ only.
You're over complicating things. You don't have to do prepareForReuse the in the custom cell.
Take a look at this.
http://www.icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/
Its pretty similar for storyboards.

Center Cell UITableView When Scrolling Is Finished

I have a UITableView, and I don't want it to stop between two cells when it stops after scrolling. So I need to move the cell which is the nearest of the Y center of the screen, when the velocity of the UITableView is less than a given value.
I'm sure I'm not the first who wants this effect, but I didn't find anything in my research. There must be a word for this that I don't know. Does someone have a link or something ?
I tried to do this on my own, but I don't find how to get the cell. I used tableView.indexPathsForVisibleRows(), so I have an array of indexPaths, but now I don't know how to call the corresponding cell. There is cellForRowAtIndexPath, but this method creates a new cell, doesn't it ?
You need to implement -scrollViewWillEndDragging:withVelocity:targetContentOffset: in your table view delegate, just as in this other question here.
Basically, this method allows you to tweak the position where the velocity scroll will halt.
I can't help you more with code without knowing the structure of your table view (cells of variable height?).
Try adding this to your ViewDidLoad:
self.tableView.bounces = false

ios - a single static header that changes as you scroll

I've been searching a ton and can not seem to find an answer...
I have a tableView which scrolls all the days of all the months. And I have a titleForHeaderInSection: and viewForHeaderInSection: which displays a section header at the top of each month-section which replaces appropriately at the top, cool. But how can I create a single header where the titleLabel changes to the proper month as I scroll (in other words, Not have multiple section headers, just one)?
I can't tell whether this should be setup as "one (big) section with a header that changes" or "a headerView that changes", or whatever... Any examples would be greatly appreciated. Thank you in advance.
You can create a static view and place it above your tableView. Then you can use the visibleCells property to determine which cells are visible. When those cells change to the cells of a new section you can update the static view that is above the UITableView.
Once you are the delegate of the UITableView you are also the delegate of the UIScrollView it is built on.
So you can catch the scrollViewDidScroll: delegate call and update your header there.
So to lay it out:
1.) Create a Header View above your UITableView
2.) Become the delegate of the UITableView
3.) Implement the scrollViewDidScroll: method to detect scrolls
4.) Inside the scrollViewDidScroll method use the visibleCells property of the UITableView to determine which cells are being shown and to update your header view appropriately.
I've never done anything like this, but I don't see why it wouldn't work.
EDIT: As Husker Jeff pointed out. It is probably easier to use indexPathsForVisibleRows to determine what is on screen. You can then use that fairly easily to determine which section/sections are being displayed. Thanks Jeff!

Resources