Change height of UITableView which is in custom UIView - ios

I have a tableview which is inside a custom class UIView. This custom UIView is then added to a different UIViewController. If I change the height of the table view inside the xib file then it corresponds and changes when the app is run. I am trying to programatically change the height of this tableview though depending on how many cells it has.
Normally i would do a tableview.frame=CGRectMake(15,29,290,height) and this would change the size but it does not seem to be working.
I have also tried the [tableView setFrame:CGRectMake(15,29,290,height)]; and this also doesnt work. I have tried it inside the method in the custom uiview that sets up the custom view and I have have also tried it within the UIViewController of which the view is added. Here is my code in the UIViewController that adds the custom uiview.
LeagueInvites* invitedView =[[[NSBundle mainBundle] loadNibNamed:#"InvitedView"
owner:nil
options:nil] objectAtIndex:0];
[invitedView setUpLeagueInvite:allInvites :obj.nUserName];
invitedView.frame=CGRectMake(0, (615 +height), 320, 29+([allInvites count]*120));
[invitedView.tableView setFrame:CGRectMake(0, 29, 290, 400)];
[self.fullScroll addSubview:invitedView];
Any help would be hugely appreciated.

Related

How to add an UIView from a XIB file as a subview to another Xib file

I am trying to add a custom UIView that I created in XIB, to my view controller in my main.storyboard as a subview. How can I do this?
matchScrollView (tag 1) is the UIScrollView in view controller in main.storyboard, while matchView (tag 2) is the custom UIView I created in another XIB file.
With the press of a button i want to have the custom UIView added to the UIScrollView as a subview. But how can i actually make it show up on display? I guess i have yet to alloc and init it, along with indicate position and such, but how can i do that? I tried different ways without success. I can create UIViews programmatically, but have yet to find a way to just load the UIView from XIB.
-(IBAction) buttonTapped:(id)sender {
UIScrollView *matchScrollView = (UIScrollView *) [self.view viewWithTag:1];
UIView *matchView = (UIView *) [self.view viewWithTag:2];
[matchScrollView addSubview:matchView];
}
The reason that I am creating my custom UIView in another XIB file instead of directly implementing it on my main.storyboard view controller, is because I want to re-use the same view multiple times. So the UIScrollView has a numerous subviews of UIViews.
I was hoping I could create numerous instances of MatchView and add them all to matchScrollView as subviews.
The issue you are having is completely normal. The way Apple designed it doesn't allow to reuse custom views with their own xib into other xibs.
Lets say you have a custom view named HeaderView with a custom xib named HeaderView.xib. And lets say you want to be able to, in another xib named GlobalView.xib, drag a subview and specify its class to be of type HeaderView expecting it to load that view from HeaderView.xib and insert it inplace. You can do it like this:
A) Make sure File's Owner in HeaderView.xib is set to be HeaderView class.
B) Go to your GlobalView.xib, drag the subview and make it of class HeaderView.
C) In HeaverView.m implement initWithCoder, if after loading the view there aren't subviews means it got loaded from GlobalView, then load it manually from the correct nib, connect the IBOutlets and set the frame and autoresizingmasks if you want to use the GlobalView's frame (this is usually what you want).
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self.subviews.count == 0) { //means view got loaded from GlobalView.xib or other external nib, cause there aren't any subviews
HeaverView *viewFromNib = [[NSBundle mainBundle] loadNibNamed:#"HeaverView" owner:self options:nil].firstObject;
//Now connect IBOutlets
self.myLabel1 = viewFromNib.myLabel1;
self.myLabel2 = viewFromNib.myLabel2;
self.myLabel3 = viewFromNib.myLabel3;
[viewFromNib setFrame:self.bounds];
[viewFromNib setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[self addSubview:viewFromNib];
}
return self;
}

One UIView in multiple TableViewCells

I have a UITableViewCell which contains an UIView.
This UIView is larger than the UITableViewCell and should also be visible in the cell above.
clipsToBounds=NO
did not work.
This does only work when I remove the content from the upper cell and set the background color to clear.
This means, the content in my upper cell covers my UIView, that's why I tried to call bringSubViewToFront, but this did also not work.
What am I doing wrong?
This is how I add the custom UIView to the cell's contentView:
CustomView *view=[[CustomView alloc] initWithFrame:CGRectMake(0, 0, [DeviceInformation getWidth], [self tableView:tableView heightForRowAtIndexPath:indexPath])];
[cell.contentView addSubview:view];
and in my CustomView I am adding another UIView, which should be then presented among multiple Cells.
Thanks!

UITableView contentOffset behaving oddly

I have a UIViewController which has multiple UIViews that enter from off the screen. Rather than initializing and designing all those UIViews in the UIViewController, I created separate nib files with corresponding .h/.m files which just get preloaded.
In one of the custom UIViews, I have a UITableView with a header UIView. I'm trying to have this header hidden under another component I have in the original UIViewController that this UIView will slide into. I initially worked this out using a sample project with [self.tableView setContentOffset:CGPointMake(0, 44) animated:NO];, but that was just in a UITableViewController.
For some reason, this does not work for a subclassed UIView. I've tried setting the tableView's content offset in both - (id)initWithCoder:(NSCoder *)aDecoder and - (void)layoutSubviews and I've been checking whether the offset was actually set in tableView:cellForRowAtIndexPath: by
NSLog(#"tableView Content Offset: %#", NSStringFromCGPoint(self.tableView.contentOffset));
but it just returns
tableView Content Offset: {0, 0}
The interesting thing is that if I set the contentOffset in tableView:cellForRowAtIndexPath: it works! But I know I shouldn't be setting it there. Could someone explain to me where and how I should be implementing this when loading a UITableView from a custom nib which is only a subclassed UIView? I can post all the code if you think it'll help.
Additional Info:
Question: how are you loading the view from the nib ? – MAB
UIView *newView = [[[NSBundle mainBundle] loadNibNamed:#"ProfileCostTableViewController" owner:self options:nil] objectAtIndex:0];
I subclassed UITableView to see when the contentOffset is being rest to {0,0}. it seems its a call in ios7 from the private method : _adjustContentOffsetIfNecessary. Since this method is not documented I don't see how to prevent it from changing the offset, so I think the workaround is to change the offset after a delay or in - (int) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section.
You're adding your view using addSubview: which triggers the methods: - (void)willMoveToSuperview:(UIView *)newSuperview I think if you put your code there it should work.

Why am I unable to access this variable I created in a nib?

I created a TutorialScreen subclass of UIView, and in a xib file I created three UIViews of that type and used them in another class by bringing those objects into UIViews:
self.tutorialScreen1 = [[[NSBundle mainBundle] loadNibNamed:#"View" owner:nil options:nil] objectAtIndex:0];
self.tutorialScreen1.translatesAutoresizingMaskIntoConstraints = NO;
self.tutorialScreen1.layer.cornerRadius = 8.0;
self.tutorialScreen1.alpha = 0.0;
[self.notificationWindow addSubview:self.tutorialScreen1];
In the xib file, each UIView has a UILabel in the middle that I created an outlet for (and linked all three UIViews to), called textLabel.
But in that class I created tutorialScreen1 in, when I do the following:
NSLog(#"%#", self.tutorialScreen3.textLabel.text);
Every time that outputs (null). Why on earth is it doing that? The label is explicitly set to "text" so I don't see why it keeps calling it null. I can't manipulate it at all because the label doesn't seem to exist.
Did you create outlet of all the views if yes then create outlet of UILabel also after when you click on any button e.t.c present that view which you want.

How refresh a single control in xib?

I have a xib with a 2 UIViews named subview1 and subview2. In subview1 a UILabel is palced and in subview2 a UITableView is placed.
I want to refresh the subview1 to change the label text at a particular time but no need to refresh tableview that time. After few operations again I need to refresh the tableview but no need to refresh the label.
How can I achieve this ?.
Thanks.
You can try next way.
Load xib by loadNibNamed and just replace wanted subview.
NSArray * views = [[NSBundle mainBundle] loadNibNamed:#"your xib name" owner:self options:nil];

Resources