Change height of UITableView programmatically - objective-c

I have a UITableView and I'd like to change its height programmatically depending on the number of cells contained in it.
Can you help me?

You can use this code
self.yourTableView.frame = CGRectMake(x,y,width,noOfCell*heightOfOneCell);

You can observe tableView.contentSize changes and bind the value to the frame, keep in mind you might run into memory issues since table view won't recycle cells

Did you tried sizeToFit or sizeThatFits:?
CGRect tFrame = tableView.frame;
tFrame.size.height = MIN([tableView sizeThatFits: CGSizeMake(tFrame.size.width,
CGFLOAT_MAX)].height,
MaximumTableHieght);
tableView.frame = tFrame;
Or try this (I don't remember which was working for me).
CGRect tFrame = tableView.frame;
tFrame.size.height = MIN(tableView.contentSize.height,
MaximumTableHieght);
tableView.frame = tFrame;

Related

NSImageView setImage won't set image

I am trying to set image for an NSImageView instance object in a Cocoa project. My code looks like this:
itemTableCellView * xx = [tableView makeViewWithIdentifier:#"MainCell" owner:self];
xx.label.stringValue = [tempCi simpleTxt];
xx.img = [[NSImageView alloc]initWithFrame:CGRectMake(48, 48, 48, 48)]];
NSImage * localimage = [NSImage imageNamed:#"menubar icon"];
[xx.img setImage:localImage];
return xx;
The variable imgis of type NSImageView. The variable localImage loads the correct image, I have checked it in the log. The variable lable is initialized correctly and gets the right value
I just keep on getting empty NSImageViews in my app.
Your NSImageView is initialized with no size (it's frame equals NSZeroRect), also you don't add the image view to any subview.
when you create your NSimageView,try doing an initwithframe:. This will set a size for the NSImageView. I believe that you might be drawing in a rect of size zero otherwise.
You should use
xx.img = [[NSImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
Or, maybe, as img is property, you don't need to init it at all?
And just use
xx.img.frame=CGRectMake(x, y, width, height);
Well, yet another quirk of Objective-C!
I found out that even when a NSImageView is a property of an NSView, it needs to be added to it through:
[xx addSubview:xx.img];
Well, played Cocoa, very well played!
cheers!

UITextView custom form

Possible to make the custom form of the UITexView? For example ellipse or semi ellipse form.
or a more complex case
Yes, try
atextview.layer.borderWidth = 2.0;
atextView.layer.borderColor = [UIColor blackColor];
atextView.layer.cornerRadius = 10; //adjust it to change req. shape
UPDATE: In case of change in contentInSet check this question and answer
No need for that, just make it transparent and put UIImageView with any image U like underneath it...

Setting UITableView ContentInset

I am changing content insets bottom value manually.
literatureTableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
This works fine but i want to set the value depending on the height of the table contents. I tried some things with tableView.bounds.size.height etc. but nothing is working.
If you need to know the calculated bottom inset prior to displaying the reloaded data, then you must calculate the heights manually.
However, if your app permits you to call reloadData and then adjust the inset, you can arrive at it much more simply by doing this:
CGFloat trueContentHeight;
CGFloat heightWithInsets = self.tableView.contentSize.height;
UIEdgeInsets insets = self.tableView.contentInset;
trueContentHeight = heightWithInsets - (insets.top + insets.bottom);
Then you can calculate the bottom inset with the trueContentHeight and the height of the tableView's bounds.
youll need to add up the height of your cells and headers (if you have any). You can use
tableView:heightForRowAtIndexPath: from self to get the height's manually and add them up if they are not all the same.

Two UILabels under each other, hide the top one

As described in the title, I have an app with two UILabels under each other. If I want to hide to top one programmatically, it's possible with myLabel.hidden = true.
If I'm doing this in Android (with myLabel.setVisibility(View.Gone);), the label under this label will move up.
But if I'm doing it for iOS, the label is hidden, but there still an empty space instead of the label.
Any idea how I can fix this?
When hiding the second one, get its frame, hide it and change the frame property of the third :
CGRect secondFrame = secondLabel.frame;
secondLabel.hidden = YES;
thirdLabel.frame = secondFrame;
do something like this:
[UIView animateWithDuration:0.3
animations:^{label1.alpha = 0;
label2.frame = label1.frame}
completion:NULL];

How to position a UIView?

I spent a couple of hours trying to position a UIView and eventually figured out that I needed to modify the views frame. So I added a 'setPosition' method to the UIViewController subclass like this
- (void) setPosition:(CGPoint)position
{
CGRect newFrame = self.view.frame;
newFrame.origin.x = position.x;
newFrame.origin.y = position.y;
self.view.frame = newFrame;
}
However, that seems so simple that I don't understand why UIViews don't have this method already, which makes me think this might not be the right way to do it. So that's my question...
Is this method OK or am I doing something I shouldn't be doing... for some reason?
Copying, Modifying and setting the frame again like you have done here is how this is generally done. This can also be done by creating a rect and setting it directly:
UIView.frame = CGRectMake(50,50,50,50);//x,y,w,h
Doing this in an animation block will animate these changes.
Alternitively you can set a views Center point with :
UIView.center = CGPointMake(50,50);
Other way:
CGPoint position = CGPointMake(100,30);
[self setFrame:(CGRect){.origin = position,.size = self.frame.size}];
This i save size parameters and change origin only.