How would I reload custom views in a scroll view? - swift

I have a scroll view that loads custom views (using a nib) from CoreData. They appear in a x by 2 grid that scrolls vertically. The problem I face is reloading the scroll view when I add or delete an item. I have tried redrawing the views once my data has been updated, but right now my solution is re-instantiating the view controller that holds my data from the very beginning of the navigation stack, thus redrawing the views as I need. The problem with this is that it creates extra views (UINavigationView) in the hierarchy, which is not a desired behavior.
How would I achieve the same result without creating extra views in the hierarchy?
This is my ViewController

Judging from your image, I would suggest abandoning your entire architecture and just using a UICollectionView. The three red rectangles would then be UICollectionViewCells. UICollectionView does a wonderful automatic job of dealing with insertion or deletion of a cell; indeed, it even animates the change.

Related

scrolling entire screen with UIScrollview and UICollectionView

I am trying to build this view where I have a UI Image view with some labels below it. And then Below this I have a Collection view which has a few items. I am struggling to figure out how I could have this view such that the whole screen keeps scrolling up once i get to the collection view.
Right now, the image view and the labels below scroll, but then the collection view only scrolls within its constraints. What I want is the entire screen to scroll up so the collection view items fill the screen up. I have seen several other replies to similar questions but am unable to follow it.
Can someone help with what I am doing wrong here ?
For the best practice on mobile programming, you should not have a scrollable view in another scrollable view. If you do so, you will have the problem as you described that your child collection view starts to scroll while other elements like your imageview and labels stay there.
To achieve your point, you can simply use a collection view and have custom cells. Now your entire window has only one scrollable view so it looks better. Then you are basically have different cell types. On top you have a collection cell as image, then you have few collection cell as labels, and so on.

UITableView is preloading every cell

I have a UITableView inside of a scrollview. I load the datasource from internet content in the background, then call "reloadData". But, it loads EVERY cell. As far as I know, it is supposed to load the cells as they appear onscreen. What is going on?
I guess it happens because a UIScrollView itself does not take care of figuring out which subviews to be shown or not, thats up to a subclass or a delegate.
From: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIScrollView_Class/Reference/UIScrollView.html
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.
And if you add a table view inside a scroll view I think the table view will load as many cells as needed to draw its bounds without knowing which parts of it that really is on the screen.

iOS - UITableView Pan Gestures

This may turn out to be 2 questions in one, but I believe solving the first will go most of the way to solving the second.
First, I have created a view in Interface Builder (I know!) and I load the xib file into my view controller in the app delegate. All of this runs smoothly and as expected.
The view consists of 3 table views, two of a similar size and one small one in a corner.
A cell is added to one table view a pan gesture is added to allow movement around the scene.
However, if a cell is moved from it's table view, it appears to go BEHIND the parent view, as per the screenshot below:
The grey line between the two tables is the gray background of the parent view. If the user still has his/her mitts on the cell, they can drag it into view but how can I make it so that all table views are on the same 'layer'?
I.e. so that dragging a cell from one table view to another with show the cell hovering over both views.
This leads me onto my second question, which I will not ask yet as I believe the solution to this will solve my current issue. but I will explain for further clarity.
In my pan gesture, I use a point inside check to see if the cell is within a table view, currently panning a cell from any table makes it print. It is almost like the views take up the whole screen, even though they are sized not to?
All ideas welcome! Thanks!
This was solved by listening for the cell leaving the bounds of the table view, when this happened, I transferred the cell across the table view that it has been dragged to, adding it to the bottom of the list.
Not the most elegant solution and you do lose the touch but it does work,

UIScrollView issue

I have a UIScrollView with textviews as subviews. Now in my app there are multiple UIScrollViews like these. And depending on the selection I display the appropriate UIScrollView on top of the previous view. This works fine in all cases except when the previous view has been a UIScrollView as well. In this case the behavior I get is of two UIScrollViews stacked on top of each other and both the views capture the scrolling events. The textViews from previous scrollView is also visible (not editable though) and overlaps and causes all sorts of issues. The thing is a full screen UIScrollView placed as subview to a previous view causes problems when the previous view is a full screen UIScrollView as well.
Any pointers on how to overcome this would be great. Is there anyway to notify the parent scrollview of the child's scroll events and move it the exact same way so this mess is masked?
Thanks!
UIScrollView documentation says:
Important: You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.
As far as I know, it is generally not recommended to embed a UIScrollView subclass instance in a UIScrollView. In one of my projects, which is an IM application, I have a UIScrollView that contains many UITableViews, it works when you try enough but it really takes a lot of effort to handle subtle bugs and make it work properly.
It is really hard to say something useful for your problem without diving in to code, but i can recommend you to not add UIScrollViews one on to another like a stack. I'd try doing it by allowing only one UIScrollView at top, and removing others. When you need to show another one, remove the top one from view hierarchy and add the new one. I wish this helps, good luck.
Found a crude solution by presenting an empty view before I present the next scrollView. Added the scrollView as subview to this empty view (with a BG image) and added the to-be presented scrollView as a subview to it and then presenting this on top of the previous scrollView.

iPhone: How to Place a Scrollview Inside of Tableview Cell

I want to put a scroll view inside of the table view cell. I have a number of buttons in one cell of table view and I need to scroll that cell to show the appropriate button.
If you want to use a vertical scroll view then I wouldn't suggest you doing it because, as TechZen wrote, there will be a mess in this case.
If you want the inner scroll view to scroll horizontally then it might be achieved by 2 ways:
Implement a custom table view cell that will include a scroll view inside it.
Add a scroll view as a sub-view to your cell that you will return from tableView:cellForRowAtIndexPath: method.
I suggest you to use the second approach.
There are plenty of examples online. Usually the sub-views are labels or image views, but it is not complicated at all to add a scroll view instead...
I don't think you can do this. It would require nesting of scrollviews which isn't really supported.
Even if it was, it would be very difficult for a user to know which scrollview they were hitting with their pudgy finger. Remember, you don't have the one pixel precision of a mouse on the iPhone. You have an area of at least 15x15 pixels. You don't have a scroll bar but instead just drags anywhere on the screen.
Instead, you should use a master-detail pattern. Selecting the cell in the tableview pushes a detail view which has the scroll view with all the buttons.
Why do you want to do it like this?
I think the best idea is to draw your table view manually above your uiscrollview. I did it, and it works. It just takes more effort and drawing accuracy. But that takes a lot of time. :)