Reloading Data in a Custom Picker - ios

I created a custom Picker with a nice X-ray lens glow effect for the target selection. It is just made up of two UIScrollViews with the same content offset. When the label goes under the lens it appears to glow. Anyways, I need to dynamically update the data and if it were a normal table view I know I would just use [tableView reloadData]. I have custom delegate methods for assigning labels / rowHeight and some other customizable features. I just need all these delegate methods to be reevaluated after a switch is pressed. Any suggestions? Thanks!!
EDIT:
I thought I was having a brain fart, and for the most part was. I can write my own "reloadData" function in the custom picker class. My only problem is having the picker call the titleForRow delegate method the number of times equal to the number of rows. I know my picker can do it the first time when the view loads. Do I really need to use a FOR loop in my reloadData function or is there something I am missing?
In my reload method I want to call:
[[self delegate] titleForRow:(int) forPicker:self];
Thanks Again!!

I was looking for a quick fix.. but sure enough the right way to do this was to use a FOR loop in a custom reload function for my class. The reload code is essentially the same as the initialization code, just removing subviews and repopulating.

Related

Is there a way to directly access UICollectionView elements without reloading?

I have another question open where I'm trying to figure out how to reload the collectionView without auto-scrolling. I was also realizing there are a lot of other situations where I will need to change things in the collection view. Also I have some items that I will want to change the .alpha on and change the text of. Is there a way to do all of this in Swift? For example (to be specific) if I have a collection view with a view in each cell and that view has a textField in it, can I change the alpha and text, (change alpha with animation even) without reloading entire table?
Look at the documentation for UICollectionView. There are several "reload" methods:
reloadData()
reloadSections(_:)
reloadItems(at:)
If you just want to reload a single item, update your data source's data and then call reloadItems(at:) passing in the index path for the item.
Another option, if a cell is currently visible, is to use the cellForItem(at:) method to get a reference to an existing cell. Then you can directly access UI components of the cell as needed. You should also update your data model as needed so if the user scrolls and comes back, the cell will be rendered properly.
Most appropriate where you can update your custom view of a particular UIcollectionViewcell is reloadItemsAtIndexPaths.
You would be handling a particular item than whole collectionview with reloadData.
You can handle it via notifications or some call backs in your code where you can make decision when to update which cell.
Hope it will help you.

UITableView how to continue scrolling after reload

I have a UITableView in my application. When the table is scrolled almost to the end, I download more data and reload the table.
If the tableView was scrolling, at the time I call [tableView reloadData]; scrolling stops. How can I achieve effect of not stopping the scroll meanwhile reloadData? I think I need to somehow save scrolling speed and then restore it, but how to do this?
P.D. I really searched this question before asking.
I thing, this method (insertRowsAtIndexPaths:withRowAnimation:) is the key.
There is a use case and a nice tip described on SO: the use case and the tip.
Since UITableView is subclass of UIScrollView, you can use UIScrollViewDelegate's scrollViewWillEndDragging:withVelocity:targetContentOffset:
method to determine how far it is supposed to scroll and then after table reload call setContentOffset:animated: with that target offset (or beginning/ending of tableview if it becomes smaller) to simulate continued scrolling
EDIT: since your targetContentOffset is probably going to be CGRectZero, you will have to recalculate it somehow using velocity from the same method
if i am not wrong you are looking for a function call Lazy load. I can recommend you to search SVPullToRefresh
here!

How to make my own UIPickerView?

Since UIPickerView can't be customized to the level I want (without taking a risk by hiding specific subviews), I need to roll my own to select a time.
I figure I can do this using two UITableViews, but I'm unsure of how to do 2 things:
1) How do I determine which cell is in the middle of the view (i.e. which one did the user select)?
2) How do I make the table snap to the cell nearest to the middle of the view once the user has stopped scrolling?
Thanks for your help.
Why do you say that UIPickerView can't be customized?. It has a bunch of delegate methods made to allow a lot of customization, such as view for rows, height for rows etc etc..
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPickerViewDelegate_Protocol/Reference/UIPickerViewDelegate.html
Implement the delegate in your ViewController and customize it as you want.
You're just trying to select a time? Then use UIDatePicker. It is a picker view for time/date.
Depending on the type of customisation that you want, you might not need to make your own UIPickerView. You can return your own views for each component using UIPickerViewDelegate. Check pickerView:viewForRow:forComponent:reusingView:
The free Sensible TableView framework has a UITableView based date picker that you could use out of the box. Should save you a lot of time as it handles all the scrolling and table view resizing. Hope this helps.

How to refresh a UITableView in iphone?

I have a UI table view loading data from network. If the user move the finger on one cell, one button will be displayed, just like iPhone style delete button. If the user click this button, the value of one label in the cell will be changed accordingly. However, I didn't find any way to make table view to re-draw this cell, except for reload the whole table. I don't want to use reload data method, because I just need change the value in one cell instead of the whole table. Any suggestion on this?
Take a look at the method
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
Calling this method will result in your table view's data source asking its delegate for information about that cell.
For future reference, I found this very easily by checking the documentation on UITableView. I suggest you do that in the future before posting here.

UITableView initial row selection

Maybe I'm missing something obvious, but I'm have a hard time programmatically setting the row selection in a tableView. The goal is to simply have a tableView open with a row already selected. The problem appears to be that I have to wait until the tableView is fully loaded before I can modify the selection.
I've read various strategies such as calling reloadData for the tableView in the viewController's viewWillAppear method, then immediately calling selectRowAtIndexPath for the target row. But when I do that, I get a range exception because the tableView has zero rows at that point. The UITableViewDelegate methods (numberOfRowsInSection, etc.) don't appear to be called immediately in response to reloadData (which makes sense if the table rows are drawn "lazily").
The only way I've been able to get this to work is to call selectRowAtIndexPath after a short delay, but then you can see the tableView scroll the selected row into view.
Surely, there's a better way of doing this?
Well, you can use another strategy. You can create a hidden table view, configure how you want and than show to user. Use the tableview.hidden = YES.

Resources