Dynamically setting size of frame of a UITableView which is placed in UIScrollView - ios

I am using swift and have a story board containing a UIViewController which includes a UIScrollView with UITableView.
What am I trying to do is to load an excel file in a table so I need to scroll horizontally. I can set the content size of scroll view with:
scrollView.contentSize = CGSize(width: 3500, height: scrollView.contentSize.height)
I do also need to set the size of frame of UITableView. When I set it from storyboard with the field shown it works, but when I set it programmatically from viewDidLoad method it does not work. The width value is shown as changed to 3500 but when I checked in tableView cellForRowAtIndexPath method, I see the value set in story board.

Here is a sample project I just created to see if I could create what you are asking for.
https://github.com/joalbright/Excel

Apple Documentation discourages you from embedding UITableView inside UIScrollView.
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.
Anyway are your UITableView and UIScrollView scrolling horizontally?

Related

Issue with Autolayout and StackView while using ScrollView

I a trying to create a UIScrollView that fits exactly the contents of its subviews. I do this with a StackView and the scroll view's height is determined by how much content there is in the stack view. Within the stack views, there are views that contain an UIImageView and a UITextView
The UIScrollView starts below the title view, however every time I want to add another view (which it should scroll when the content is bigger than the actual frame) there is an issue with the scroll view's Y position. This works perfectly if I only add UILabels and UITextFields, using the same procedure
How can I do it so I can programmatically add views on the stack view that includes an UIImageView and a UITextView, just as the View Controller with labels and textfields does.
You can download my app project at the following url
https://github.com/francisc112/DescriptionWithImageApp.git
Instead of a scroll view why don't you use a table view and create a custom cell with the stackview and its contents inside the cell and you can dequeue the same cell for adding multiple information.
I was not able to open your project
So I Tried opening your StoryBoard and Re-Viewing your constraints Applied , here is what I have Done Please check in following Link
Link - https://drive.google.com/file/d/1D-dzKsSqpx7dWsI5fYRdAfCMY_vENoIR/view?usp=sharing
View Hierarchy
Note - For such output I will prefer using a TableView instead of StackView as Same output can easily be achieved using TableView

UITableView inside UIScrollView - Cell not clickable

I have a problem with a UITableView not detecting touches.
In an iPhone-only app, I have a UIViewController, which containts:
UIScrollView
UIView (let's say a content view)
Some labels
UITableView (last)
The labels have a dynamic height, because they contain some text that I must retrive from the web, so I'm using auto layout.
I'm setting the content size of the UIScrollView inside the viewDidLayoutSubviews method, and I'm doing this by summing UITableView.frame.origin.y and its height.
Here comes the problem: when the labels contain only some words and the UITableView does not exceed the iPhone screen size, everything works ok. But when they grow, and the UITableView gets pushed down, when I scroll down I can't click on the cell anymore. Also, if when loading the view the table is half visible and half not, I can click the visible cells, but if I scroll down, I can't click the others.
I'm using swift 2 and Xcode 7.
Here is an example:
Clickable:
Unclickable:
Do the following thing:
yourView.clipToBounds = true
Now, if UITableView does not appears means your UIView is not same bigger to hold down UITableView.
Make sure that your UIView height is bigger to hold the contents in it and then try to tap on it.
Updated:
If you are using AutoLayout, then do the following thing.
Give the fix height to UIView
Take the outlet of height constraint of UIView
Now, in viewDidLayoutSubviews change the constraint of UIView to UITableView contentSize height.
self.heightConstraint = self.tableView.contentSize.height
Let me know, if this helps!
Adding some info to the #Rémy Virin's answer here:
From Apple Documentation, you shouldn't embed a UITableViewinside a UIScrollView.
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.
Addition:Solution Since you want to scroll the content above table also, the one solution is place a Header View for the Table and place the Content over there to make it Scroll along the Table View.
If you use Autolayout, no need to specify contentSize to UIScrollView as it will automatically expand by taking the height of its subview.
In your case, increase the height of the Base UIView (content view as mentioned by you) which holds the tableView when the tableView height increases.
UITableView becomes unresponsive when it extends below its container view.

How would I create an "About" page like this for my app? How do I have a UITableView (grouped) under a UIView?

Take this about page for Things:
I'm having trouble creating something similar. I just want a UITableView under a UIView with a UIImageView and a UILabel in it.
If I use a UIViewController and so I can position the UITableView downward, I get this error: "Static table views are only valid when embedded in UITableViewController instances."
If I use a UITableViewController with a grouped style and use contentInset on self.tableView to move it down ([self.tableView setContentInset:UIEdgeInsetsMake(150,0,0,0)];) I can't figure out how to place a view above it. If I try to attach anything to self.view it crashes (obviously). Same happens if I attach anything to self.tableView.
I then tried making the UIView the header of my UITableView section (I only need one section) but I can't get it to move up enough. It just sits inside the UITableView almost.
How do I have a UITableView (grouped style) exist with a UIView above it?
This can be achieved easily using the tableHeaderView property of UITableView. If you are using Interface Builder (which it looks like you are), then you can just drag a UIView above the table view and it will be set as the table's header view. All you need is a UITableViewController; no need for UIViewController and manually laying it out.
That's because the view probably isn't placed on top of the table but rather within the table's section 0 header. Or, even more likely, the view in question is just a regular UITableViewCell with a 0 alpha background.
Either of these options would allow the top view to be scrolled out of frame as the user scrolls under every condition.
I recommend [MDAboutController] (https://github.com/mochidev/MDAboutController)
It's easy to integrate and you don't have to waste any time configuring the UITableView.

UITableView and Another View inside ScrollView

I have UITableView and UIView(used to show image slideshow) inside UIScrollView. The tableview is dynamic and i want to automatically adjust height of table view to show all the cells(and disable scrolling of tableview) and scrollview should show both slideshow and tableview and could be scrollable, I know how to do this with code but i want to know if is possible to do in Interface builder(without using iOS6 auto layout) using previous(iOS5 and early) autoresize method in the xcode size inspecter tab(see image)
I think this is hard to do only using interface builder.
You can set your objects via interface builder. But for maintaing scrolling conditions you need to do it programmatically. Also for table view height to be dynamic you need to be done programmatically.

How to implement this view for iPhone

The view I expected is like this image
The main view contains three parts: one label, one webview and one tableview. But the difficulty is their size are not fixed. I hope the webView can auto strech height according its content; the tableView's is similar to webView and its height should be decided by its content.
If webView's height is bigger than screen's height, I want to scroll the whole view not the webView.
I tried many ways but can't get the expected result.
You can achieve by using UITableView add your all component on UITableView cell by using cell.contentView and add table on your UIView.

Resources