In my project, I am adding a UIView to UITableView and adjust the UIView's frame when the contentOffset of UITableView changes so that there is an illusion of a floating view above the tableView's contents.
Users of the library now report that this tequnique does not work when the UITableView is created with static cells through a storyboard.
I did a bit of research on this and found that in particular, the tableView.contentSize becomes {0, 0} as soon as you call [tableView addSubview:...].
Apparently, this is not a problem with dynamic cells, but with static cells, the contentSize stays {0, 0} until you call [tableView reloadData]
Do you have any ideas why the UITableView(Controller) is behaving like this with static cells? And any ideas how to fix this without calling [tableView reloadData]?
You are using a UITableViewController. This means that the view property of the controller is the table view, and the table view is a bit touchy about adding other views.
Instead, you can go one of two routes: you can transform the controller into a plain UIViewController, add a table view and declare and implement the datasource and delegate methods for it. Then add your other view to the view rather than the table view.
You could also try to add the view to the window. This should work, but maybe you need to tweak the positioning if it should be relative to the visible view.
Related
I am looking for a third party library that would allow me to use a UIScrollView with a UITableView mechanism, so it will have something like viewForRowAtIndexPath, numberOfRowsAtIndexPath.. reusing views. I know a UITableView is a subclass of UIScrollView, but I want to do more customization that will be kind of hard when using UITableView.
Since UITableViewCell is a subclass of UIView, you could use a custom UITableViewCell, adding your view as the UITableViewCell's subview. I am assuming that all your views are of the same size. (In the table view controller's init method, remember to set self.tableView.rowHeight to a value that will accommodate the heights of your views.)
See the Customizing Cells section of Apple's Table View Programming Guide for iOS for more info.
I'm trying to add a UIView on top over the UITableView to mimic the iPhone Facebook style menu. I have it working fine by making the controller a UIViewController then adding a tableview however I am unable to make the menu a static menu unless the controller is a UITableView.
Is it possible to add a view ontop of a tableview and only make the tableview in the background scrollable without the view in the foreground scrolling?
Here is what I have with the subclass being UIViewController
But I am unable to make the tableview cells static via IB since it is not a subclass of UITableView Controller.
EDIT per NSJones Code:
It seems to be going somewhat in the right track. However the view still blocks the table. If I remove the view from the storyboard it will only display the table.
You can make a view hover the same way you make any real thing hover; Hold it up with something invisible.
Basically what you want to do is create a clear UIView (with user interaction disabled) that is the size of your view controller's view, and add it as a subview to your view controller's view property. That way it sits invisibly on top. then you can add a subview to that clear view and that subview won't move.
Edit:
It seems this nice clean approach won't work for you since you need your view controller to be a UITableViewController. The answer for this slightly more complex approach is to use a delegate method for UIScrollView which also works for UITableView. Apple has a fantastic demo of this concept in the WWDC2011 - Session 125 - UITableView Changes, Tips, Tricks video. If you can watch it I highly recommend it. The meat of this issue begins at about 36:10.
But to sum it up you implement the scrollViewDidScroll: delegate method. And handle the movement of the tableview by adjusting the position properties of the view. Here I am keeping an UIView property named viewToKeepStill still using this method.
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
// CGFloat stillViewDesiredOriginY; declared ivar
CGRect tableBounds = self.tableView.bounds; // gets content offset
CGRect frameForStillView = self.viewToKeepStill.frame;
frameForStillView.origin.y = tableBounds.origin.y + stillViewDesiredOriginY; // offsets the rects y origin by the content offset
self.viewToKeepStill.frame = frameForStillView; // set the frame to the new calculation
}
Instead of adding it as a subview of the table view, add it as a subview of the superview of the table view; that way it won't scroll.
So instead of this:
[tableView addSubview:viewController.view];
Do this:
[tableView.superview addSubview:viewController.view];
Assuming you want something that is visible full-time with the table, start with a view which contains both the menu view and the UITableView. Make the table smaller so it ends where the menu view begins. The table view can work with less vertical space.
If you have your UIViewController's view to be your table view then your table is going to span over the whole screen, so you won't be able to add anything on top of it.
Why not try the following:
1) create a new UIViewController
2) add a view on top where you want your menu
3) in the space left under just drag a table view from the component library
4) don't forget to set the 2 table view delegates to be your view controller class
that's about it?
I'm trying to insert a view behind the cells of a uitableview but I can't find a way to do it properly. Everytime I add a subview to self.view or self.tableview, it goes on the foreground, even if I use the method "sendSubviewToBack:" ...
Do someone have an idea on how to achieve it ?
PS : I don't want to use self.tableview.backgroundView because the view is fixed.
Thanks
I solved it by calling [self.view sendSubviewToBack:self.myBackgroundView] in tableView:willDisplayCell:forRowAtIndexPath:. This will put the view behind each cell.
What you experience is logical. The self.view of a UITableView is indeed the table view. If you insert a subview, it is inserted on top of the table view - and there is no way to send it to the back.
Solution 1
The most flexible solution is to switch to a UIViewConntroller and implement the table view behaviour yourself. You need to
insert your own table view as a #property. This is now a subview of self.view. You can do this in code or in IB.
declare and implement the <UITableViewDelegate> and <UITableViewDatasource> protocols
set the delegate and datasource properties of the table view to self
insert any other subviews at will and shuffle them around as you wish.
Solution 2
If you just want to display something behind the tableView you might be able to use the table view property backgroundView. This will display behind the cells, but you will have limited control over the view's size (which you could again solve with further subviews of the background view). Also, you need to make sure your cells are transparent.
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index
with index 0 will take the View to position which is the back-most one.
This is a method on UIView.
Since UITableView Inherits from UIScrollView : UIView : UIResponder : NSObject. I guess this would help.
I have a grouped table view.
I'm trying to create a custom cell using interface builder, however when I'm putting a label, or a view as a subview for the cell, and stretching it to the entire cell, when I run the app the label goes beyond the cell's dimensions.
Any reason why it would do that ?
I tried to play with the resizing mask to no avail.
When the table is plain, there's no problem.
I guess I'm doing something wrong, cause it's not suppose to be that complicated.
I don't have much experience in creating UITableViewCells with IB, but I would recommend adding your subviews programmatically using a UITableViewCell subclass. Your issue is that the subviews are added directly to the UITableViewCell view, and the left and right margin are part of that view.
The UITableViewCell subview that represents the actual "active" space (the white part of the cell) is contentView. If you add a subview to contentView, then it shouldn't appear outside it. In other words, CGPointZero of contentView is the top left point of the white space.
I'm trying to add a UIView, in particular a UIImageVIew, after (that is, below, but not in a z-index sense) a UITableView but before (that is, above) a UITabBar.
You know, the typical "banner/adv space" that you can see everywhere.
My main problem btw is that i don't know exactly where to put it, as a subview of wich view specifically; the UITableView resize automatically according to the space left from the UITabBarController's main view height, minus the height of the tabbar.
I would like it to be put inside the UITableView instead of somewhere else beacuse it is more related to the content of the UITableView, but i have all the autoresizing problems of above. I've tried playing with autoresizingmask, and with the autoresizesubviews flag, but without success. I've even tried the footer ot the UITableView, but that is not fixed in position, it scrolls away if the table is long (expected, and normal).
Is there a way to add a subview in that point, stretching the table itself correctly?
Thanks everybody.
Use a normal UIViewController instead of a UITableViewController. Use the view controller's view as a container view in which you place the table view and the image view. Your view controller can still act as your table view's delegate and/or datasource.