How to achieve this in an UIScrollView? - iphone

I want to display a list of items inside a UIScrollView , here for some items i'm having an images and some won't. The items having image have to be displayed along with image and the others just as a list of items, how can I achieve this? I mean, how to take care of contentSize, offset and all.
Any help is appreciated in advance.

Don't re-invent the wheel. Use a table view. This is a subclass of UIScrollView, optimised to display a list of items. You can use different cells for items with images and items without.

It really depends of what you want to achieve. You have to take in consideration, that sometimes is just better to have a UITableView with some custom UITableViewCell (for memory purposes).
Ok, so lets assume you want a UIScrollView with a vertical scroll. You will have items with some random images and others with no image at all. What you have to do, is just keep adding your items one after another inside your UIScrollView. You can do this with a while cycle. The only thing you have to pay attention is the measures of your items. So lets assume you have items with 140 of height. You can do something like this (I did not compiled this code):
float yReference=0.0f;
while(MyItem *item in arrayOfItems){
[myScroll addSubview:item.view];
item.view.frame=CGRectMake(0.0f, yReference+20.0f, item.view.frame.size.width, item.view.frame.size.height);
yReference+=140.0f;
}
Ok so now you have all your items added to your UIScrollView with space of 20.0f between them. After the while, you just set the correct contentSize of your UIScrollView:
myScroll.contentSize=vReference+20.0f;
(I added again 20.0f just to have a bit of space at the end)
You can check this tutorial if you need more help. Although he is adding the items horizontally the logic is the same.
P.S: In order for you to get more help, you should increase your acceptance rate. :)

Related

tvOS UICollectionView scrolldirection

I am currently playing around with the tvOS SDK.
I need a collectionView with multiple section and many items, but only one row per section. So the user can scroll vertically through the sections and horizontally through the items in the single row.
When I set the scrollDirection of the collectionViewFlowLayout to ".Horizontal", the sections and the items are all just in one row, which looks pretty shitty.
Is there another way to get this behaviour? I thought about multiple collectionViews in a scrollView, but this seems too complicated and overheaded for me.
Thanks in advance!
Cheers.
I would think the best and possibly only way to get around this will be to a table view of collection views.
Somebody correct me if I'm wrong but that's the way I would probably go about doing it. Hope it helps!
I was having a similar problem, and it turns out the Collection View I was using wasn't tall enough.
It should be tall enough to accommodate two cells on top of each other plus min spacing in between, and if it is tall enough then the Collection View Cell will appear in the top left corner of the Collection View in storyboard.
Once it is tall enough setting the flow to horizontal should work the way you want it to.

Custom UIPickerView - Increase the no of items shown without scrolling

Picker normally shows 5 items without scrolling. Is there a way to increase the size of the picker to show up to 8 items without scrolling. Please help.
Kind of. UIPickerViews can only be a certain number of points tall (easy enough to find out what those are yourself), but you can fiddle around with the height of the items in the UIPickerView using the -pickerView:rowHeightForComponent: delegate method. If you return a small enough number, you could fit about 200 items in a UIPickerView all at once. They'd all be a single point tall, but you get the idea...

Better to use UIScrollView or UITableView for horizontal buttons?

I have a page enabled scrollview on an iPad. On the first page, I have a child scrollview that scrolls horizontally through image buttons. The buttons scroll the outer scroll view to the correct page. Its basically like a table of contents that jumps to the correct page.
My end goal is to be able to categorize the buttons seen in the child scroll view. So there would be a segmented control that changes what buttons you can see. So maybe one category would be ALL, and another category would be A-M, and another would be N-Z for example.
My question is, should I use a uiscrollview or a uitableview?
Right now I use a scrollview and it is really easy to get the buttons in. I could implement the different categories kind of gimmicky by having all of the buttons in the scrollview and then just showing or hiding the buttons accordingly. I feel that it'd be bad memory usage though.
For a uiscrollview i was looking at using EasyTableView, butI'm not 100% sure if this is compatible with what i want to do or if it'd even be better.
Any ideas for what the best way to implement this is? Specifically, I'm not sure of the best way to change the buttons when I change categories.
Thanks!
Use a tableview when you are dealing with data that is best expressed as sections and rows.
I think for your situation I'd have a UIView subclass that can display the images you need for a given category. Stick that on the outer scrollview as needed. You can keep memory low by only keeping the currently visible view and the ones on either side on the scrollview. When you scroll to a new location you can recreate the view needed for that page, and the ones surrounding it. Then you release the ones that are far away and let the system reclaim their memory if needed.

How to achieve 2 dimensional scrolling on iPhone

I am trying to create a excel like view in my app. To do that I wanted to achieve both horizontal n vertical swipes at the same time .i.e., if the vertical scroll would fill with rows and the horizontal with columns.. have no clue hw to begin with.
This is an interesting question. For sure I would say start with UIScrollView, and set the content size bigger than the screen size, exactly how big is up to you.
Next you'll want to make some kind of implementation that efficiently figures out what cells are on screen. If I were you I would do something similar to how UITableView is done, i.e. make a custom object that keeps track of what indexes should be on screen based on the offset of the scroll view.
Next you'll need this custom object to either directly or indirectly get the visible cells to display themselves on screen when they are visible, and remove them from the superview when they aren't. I'd also recommend reusing the views that are offscreen.
This is a moderately challenging thing you are trying to make, so be sure you are very organized and don't be scared to make a few new custom object to perform specific tasks.
Sample interface file:
#protocol doubleTableViewDelegate
-(NSInteger)heightForRow:(NSInteger)row;
-(NSInteger)widthForCol:(NSInteger)col;
-(NSInteger)numberOfRows:(NSInteger)rows;
-(NSInteger)numberOfCols:(NSInteger)cols;
-(UIView *)cellForRow:(NSInteger)row col:(NSInteger)col; //get the custom table view to store and dequeue these, i.e. store them in an NSSet when they go offscreen, and give them back when a certain function is called.
//There's probably a few functions missing here still.
#end
#interface doubleTableView: UIScrollView {
id<doubleTableViewDelegate> infoDelegate;
}
//your functions to show and remove cells as needed
#end
You'll need to work out the rest, but keep updating this page and I'll try to help out.
Good luck with this, let me know how it goes.

Do I need to remove subviews that aren't visible in UIScrollView to keep it running smoothly?

I'm working on making a special UIScrollView to show a timeline of events, (Like a gantt chart if you're familiar with such things) and there could potentially be dozens of these events, some visible and some not at different times.
What I'm wondering is this: should I make an implementation similar to UITableView to remove the items that have scrolled out of sight, and reuse the views for other items as they come into the screen?
It seems like it could take some work, and I don't want to waste time on it if this isn't something that will affect performance on a small scale.
I'm getting the idea from the UIScrollView class reference:
"The object that manages the drawing of content displayed in a scroll view should tile the content’s subviews so that no view exceeds the size of the screen. As users scroll in the scroll view, this object should add and remove subviews as necessary."
Thanks for any help.
It depends on how much memory each item in the scrollview requires. You cant really tell unless you build it or provide more information about exact amounts and what each element in the UIScrollview contains. Removing items that are not visible is not a massive job but it can have some problems. I would first try it without removing them and optimise the elements that you add to the scrollview so they use as little memory as possible and then use instruments to check your mem usage. Then go from there. Hope that helps.