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!
Related
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
I have an uicollectionview with supplementaryView for each section.
In each supplementaryView I have some buttons.
In dataSource method collectionView:viewForSupplementaryElementOfKind:atIndexPath: I'm setting the buttons tags with the indexPath.section. I made some delegate methods when a button is pressed and send the section(button.tag) as parameter.
Everything works fine, but here comes the problem:
when I insert or delete a section it must update the buttons tags, but I don't want to reload all the items from each section.
I've tried:
to get the supplementaryViews with the dataSource method collectionView:viewForSupplementaryElementOfKind:atIndexPath: and call layoutIfNeeded method for each supplementaryView, in this way the supplementaryViews are reloading, but this approach multiply my supplementaryViews.
a fast for in collectionView.subviews and get just the supplementaryView and call layoutIfNeeded method, but it doesn't reload, just the layout is reloading.
reloadSections: method, but this reload all items from the section
Ohh.. And I use NSFetchedResultsController(delegate) for inserting and deleting sections.
Can anyone help me with this? Is there a way to reload only the supplementaryView? Is there other approach for this scenario?
Thank You!
To reload supplementary views you can use -invalidateLayoutWithContext:.
See documentation for UICollectionViewLayoutInvalidationContext and method -invalidateSupplementaryElementsOfKind:atIndexPaths: (available since iOS 8).
Instead of reloading these views, getting a reference to each visible supplementary view so that you can change the tag might be sufficient for your needs.
There is a simple method you can use to do this:
let supplementaryViews = collectionView.visibleSupplementaryViews(ofKind: "identifier")
https://developer.apple.com/documentation/uikit/uicollectionview/1618026-visiblesupplementaryviews
So Path uses this type of page where there is a view above their customized looking table that is a background photo, which contains some user info among other things. I'm trying to recreate something very similar to this.
So lets say that I hypothetically wanted to make a view that shows exactly the way the Path app does, but instead of that weird customized version of a table view that they have, there is an actual table. How would I do something like this? The reason why I would need there to be another UIScrollView embedded into the view is because the entire thing needs the capability to scroll. I'm trying to be as detailed as possible, but its a little difficult to explain.
What I'm imagining is going to happen if I just tried it right now, is that I'd embed a UIView above a UITableView within a UIScrollView that's the size of the frame, and when I'd go to scroll, the user would only scroll the UITableView, and not the entire thing at once. Hopefully that helps convey my doubts.
Another possibility is that I'm totally over thinking this, and I can simply just subclass a view in the header of a UITableView and it would stretch the width and height that I'd like. Hopefully this is the way as this would be easy!
Anyways, can anybody weigh in on this?
Path just uses a normal UITableView with UITableViewStyleGrouped.
The custom view at the top is the header of the first section of the table.
They also access the UIScrollViewDelegate method of the UITableView to change the look of the view (I think the image is moved) when the scroll view scrolls.
If you'd like a tableview that only scrolls within a part of the view and other stuff above it then you need to use a UIViewController. Then you can make it conform to UITableViewDelegate and UITableViewDatasource and add a UITableView and make the view controller the datasource and delegate.
Then you can also add a UIScrollView to it as well.
I am trying to support infinite scroll in tableview.
I know tableview lets scroll infinitely, but my case is different.
Initially, I will load 30 items in my tableview as the user scrolls down close to 30th element, I will make a http request to get next 30 items so its not like I am loading 60 items at once.
My way about going to do this is when my table is initally filled with 30 items, once the footerview is seen, then I will request the next 30 items.
My question is how to detect footerview is seen? Is there something like 'will appear' for just footerview?
Let me know if there is other better way of completing my scenario above
Thanks!!
A UITableView is a subclass of UIScrollView, and UITableViewDelegate conforms to UIScrollViewDelegate.
So you can set yourself as the tableViewDelegate and implement scrollViewDidScroll:. You can then either check the scrollView.contentOffset yourself, or call [myTableView visibleCells]. I am sure there are various other methods as well.
The behavior you are trying to implement is known as pull-to-refresh.
There are several implementation of it already available, e.g.:
https://github.com/leah/PullToRefresh
https://github.com/enormego/EGOTableViewPullRefresh
Give it a try, or read the code the learn the details. If you are curious, the way the trick is done is by implementing the delegate methods for the scroll view and checking the value of scrollView.contentOffset.y.
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.