Detect jump using a UITableViewIndex - iphone

How do you know that you scrolled using the UITableViewIndex ?
I have some basic info to show on my cells, and when I jump/scroll I need to retrieve info about thoses cells by a request for a group(page) of items.
So in order to request only thoses I've stopped to, I need a way to know that I've stopped scrolling (I used scrollViewDidEndDecelerating) but with the UITableViewIndex we ca just jump to another index or scroll though them.

The only feedback that you get is in the UITAbleViewDelegate protocol - none of these report that the table view has scrolled.
However, you will get calls to tableView:cellForRowAtIndexPath: calls to your data source - this will tell you that the table view has moved enough to want a new cell.
Also, you have the visibleCells property of the UITableView itself to see where you are in the table?
It might help if you put more detail into your question - why do you need to know that a scroll has happened?

The UITableView inherits from UIScrollView so you can also implement the UIScrollViewDelegate methods such as scrollViewDidScroll. When the table view scrolls, these will be called.
However, if you explain why you need to know whether the table view has scrolled, there might be a better answer.
Edit:
From your updated question, it's still not totally clear whether you need the scrollview delegate methods.
If you just need to update the contents of the cells themselves, put that in cellForRowAtIndexPath and the table view will automatically call it when the cell comes into view.
If you need to update something outside the table view, you might still be able to do it in cellForRowAtIndexPath or you can handle scrollViewDidEndDecelerating: and then use the table view's visibleCells or indexPathsForVisibleRows methods to get the list of cells currently visible.

Related

Trying to Create a mostly custom TableView

I'm trying to make a custom table view. I have a UIScrollView loaded up with UIViews. I would like to recreate the reusable cell functionality that a tableview has. And I'm wondering what might be a good way to do this.
I was thinking that when the UIViews are scrolled off screen I'll remove them from the Subview, and when I need another I'll just add it. However I'm not sure what method might be the best to do this in.
I could check to see the views location in scrollViewDidScroll: but I'm not sure if this would be the best thing to check over and over again. If anyone has any suggestions or helpful tips that would be really awesome.
Thanks!
One reason why scrollViewDidScroll is less than ideal choice is that your scroll view subclass will then need to be its own delegate. That will deny the users of that class the chance to be the delegate (easily, at least).
The better place to check this is in overriding layoutSubviews. And yes, like scrollViewDidScroll, it gets called a lot! But if you think about it, the only way to detect with certainty if any subview has been scrolled off, is to check when any scrolling occurs.
So the key is to be as efficient as you can checking. The first thing I'd try is fast enumeration of the subviews, asking if each frame transformed by contentOffset falls within the parent's bounds (using CGRectIntersectsRect). If not, add it to your reuse pool and remove it from superview.
Subclass UIScrollView and override setContentOffset. That's where the scrollViewDidScroll message comes from in the original UITableView, and that's where you can implement your own logic.
Edit: you might want to study the source code for PSTCollectionView, which is not a custom UITableView but a custom UICollectionView.

Only update visible table view cells?

I'm trying to load a table view with cells that contain images downloaded off the internet. I'd like to only perform the download operation on cells that are currently visible instead of downloading every image as the user scrolls down quickly.
I found the indexPathsForVisibleRows method and that should fit my needs nicely but I need a way to figure out when the table view is no longer scrolling. My idea is to use the indexPathsForVisibleRows when the table view isn't scrolling so that all of the images currently on screen are loaded without unnecessarily loading images.
Are there any methods that I can override or notifications to subscribe to that will alert me when the table view is no longer scrolling?
Thanks!
Take a look at the methods listed here:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIScrollViewDelegate_Protocol/Reference/UIScrollViewDelegate.html
If your view controller is the delegate of the UITableView, these will also be fired for it, as the UITableView is a subclass of UIScrollView.
Particularly, pay attention to: scrollViewDidEndDecelerating. This is the one I used to do pretty much what you're asking.

How to fix the header and first row in a UITableView

I´m new trying to make some apps using objective c, so I´ve an idea using uitableview but I don't imagine how can I get this.
I´m trying to do something like you do in a spreadsheet where you have a fixed header and the first column too
So when scroll the uitableview vertically the header will stay visible at top of the table and rows will change
And finally when you scroll in horizontal direction the first cell of the row will stay visible and will change the header depending of how you scroll the uitableview
I hope you could give me an idea how to get this, because I don't imagine how to do this, also I don´t have a lot of experience with this programming language.
Thanks!
In a non-grouped table, section headers "stick" at the top of the table as the table scrolls. You can provide a custom UIView (or sub-class thereof) for a section header through the delegate method –tableView:viewForHeaderInSection:. This header view could be created on-the-fly programmatically or loaded from a NIB file. Either way, you can have it contain whatever you want, even update it as the app runs (provided you have given yourself access through ivars or class variables to the views contained in your header view.) If you go this route, you'll want to be clever about allocating resources that comprise this view, so that you are not constantly allocating new resources! This delegate method can be called frequently, and on all but the first call you could simply return the previously created (but updated as and if necessary) header view.
UITableView isn't designed to do this, although I am sure you could figure out some way eventually.
My approach would be to use a fixed UIView of some sort (possibly a UILabel, etc) in a UIViewController's nib as the header/locked cell, and add the UITableView under that. You couldn't then use a UITableViewController, but would have to implement the delegate and dataSource methods in your UIViewController, and use a UISwipeGestureRecognizer to pick up the gestures from the tableView and update the other views.
I've done this by adding a UIView that mimics the first cell in my table. In my case I am using a subclass of UITableViewCell, but that is perhaps not relevant. Normally this view is hidden with an alpha of 0.
If you view controller is the delegate of the UITableView then it will also be the delegate for the inherited UIScrollView. So in your view controller you can implement scrollViewDidScroll. When the scrollView's contentOffset is positive I set my custom view's alpha to 1 (I also do some small size tweaks to make sure there is a perfect match), and when the contentOffset returns to 0 or negative, I reset the alpha back to 0.
prepend the first row of data in your array to what ever in the first row is your headings, put the text in bold with attributed text, It wont be sticky but you will have headings...

how to fetch data when user comes to the footerView in table(iphone)

I have made an uitableView and I am fetching data from json from a webservice
now i want that I get first 20 object first and load it in my tableView. it is happening properly
Now I want that when user scroll to the footer ..next 20 data should be fetched and it should be shown in tableView..
I am stuck here ..can anyone give me any idea how to proceed next?
May be you can put Load more button in footerview and by tapping that you can load another 20 records from server ?
Another method is to implement Pull To Refresh and modify it as per your need.
You can use a ready made implementation of a UITableView with pull-to-refresh functionality.
Have a look at EGOTableViewPullRefresh or for a simpler implementation to PullToRefresh.
If you are interested in implementing this yourself, one approach is to add a subview to your UITableView (e.g., UILabel showing the "pull to refresh" message); then "intercept" the delegate methods when the user scrolls down (or up) and calculate whether the subview came into the viewable frame. Then issue the reload.
Another approach would be adding a last row to your table; then when the tableView:cellForRowAtIndexPath: method of your data source is called, return a cell with the "pull to refresh" message and do the refresh.
But you will be better off using one of the suggested implementations.
To know if you reached the button of the table (aka: seeing the footer) I think you could use this:
How to know when UITableView did scroll to bottom in iPhone

referencing a control in table view cell on viewWillDisappear

When a view will disappear I would like to collect the values of some of the switches in my table view, I have pointers to the index path, cell, and control how do I get a pointer to the table view so I can properly point the cell pointer... if I'm thinking correctly I should be able to then get the value of the control pretty quickly throught these pointers... unless somebody has a better idea. If I'm thinking incorrectly I would still like to know how to point to a specific tableView.
Thanks,
Thanks for the answer, it is a regular view, so I made a property of UITableView, and then I used the interface builder to connect thisTableView property to the actual tableView.
If there's a better way let me know. Thanks,
Wjy don't you have a pointer to your UITableView? Assuming you are using a UITableViewController the class provides one for you (self.tableView). If not you probably should just add a property in your UIViewContoller sublcass for the tableview and put it there.
The table view is, after all, one of the large objects that view controller is controlling and it lives across multiple method calls. Representing your relationship to it with a retained property asserts ownership, just make sure to release it in dealloc and in viewDudUnload if it came from a nib.
Having said that, currently you can generally find a UITableView from a UITableViewCell by asking the UITableViewCell for it superView. This is a bad idea since there is no guarantee there won't be some intermediary cells in the future, since hierarchy of cells you did not layout yourself is an internal implementation detail (a lot of apps broke on 3.0 because they walked around inside Apple's view hierarchies and something changed).