I need to decrease the table view cell of navigation controller to 280*35. I have tried to with cgrect, but it's not working...
How can I set it?
The width of a UITableViewCell is always determined by the width of the UITableView that contains it.
You can change the height of your Cell by using UITableViews delegate message tableView:heightForRowAtIndexPath:
You can set row height programmatically, as tonclon pointed out, or, if all rows need to get the same height, set it in Interface Builder as a propery of the UITableView, in de Inspector window under the size tab. It says "height" right at the top, that's the row height.
This is easier and (presumably) faster.
Related
I have worked with dynamically sizing UITableViewCell before. But currently I am facing a problem statement where I am unable to achieve dynamic sizing using autolayout
My cell has three components viz-a-viz one UIImageView, two UILabel
The structure of the cell is as follows
UIImageView has to be of fixed width and height in my case 60X60
UIImageView will be vertically centered inside the UITableViewCell
UIImageView is having a leading constraint of 15 from the UITableViewCell
Now the two UILabels are vertically placed
Both the labels can have dynamic height and I want them to push expand the cell using autolayout but I am only able to achieve keeping the top UILabel of constant height and the below UILabel increases in height.
I want both the UILabel to expand vertically and increase the size of UITableViewCell.
Just use StackView for both label and make both labels numberOfLine to 0 and line break should be WordWrap. give leading,trailing,top and bottom constraints to stack view. for spacing between labels give spacing for stackview on xcode detail pannel.
See the Screenshots for the setting of the constrains.
Add a height constrain to the label with the constant greaterthanEqualTo a fixed value (see screenshot 2). and set the lines to 0. In the table view heightForRowAt indexPath set that to UITableViewAutomaticDimension .
I have tried to search online for resize the UITableView(not UITableView cell) so that when I click the button, the tableview will minimise while the contrast of two button get closer.
TableView does provide ".hidden" function to hide itself, but it will leave a big blank space between those 2 buttons in User Interface.
So I plan to minimise the height of tableview while hidden it, but I only can get help to resize "UITableView Cell" and not "UITableView" itself.
Does there have anyway to resize TableView or adjust the UI contrast programatically?
If you views are frame based, you can simply set the TableView as hidden, and move the second button up by setting second button's origin.y like:
origin.y -= TableView.bounds.size.height
If you are using auto layout, simply change TableView's height constraint to 0 and change second button's top layout the same way as frame.
A more simpler way if you are using auto layout is, make the second button's top to TableView's bottom, so when you set the TableView's height constraint to 0, the second button will automatically move up.
Remember, when you are using auto layout, you change the constraints like how you change the frames. They are just different concepts, but internally, auto layout engine will translate the constraints into frame values.
Get the Constraint Of TableView Height from Storyboard.
#IBOutlet weak var tableView_Height: NSLayoutConstraint!
After that set the value of this table view constraint to 0
tableView_Height.constant = 0.0
apply following steps
1) take tableHeight constraint outlet
2) in numberOfRowsInSection.
if rowcount is equal to zero then set tableviewHeight constant to zero before return the rowcount.
3)if you want to set tableheight according its row count then set tableviewheight constant equal to ((totalrowcellheight)+fixsomespace)* in numberOfRowsInSection method. and aply some limit like if (height>300) then put it static height = 300
I have a uiview with a uiscrollview as a subview in a nib.
Then, I load another uiview with a uitableview as a subview of this other uiview (among other elements inside this view) as a subview of the uiscrollview.
That is, UISCrollView -> UIView -> UITableView (and some other views inside the UIView).
And I need to do the following:
Once I know the final height of the tableview (depending on the number of cells and height of them) I need to resize the uiview containing it and, in turn, resize the contentSize property of the containing scrollview.
But I don't know at which point the final size of the tableview is known (because it can dynamically change size depending on the amount of text its cells will hold) and neither do I know by how much will the tableview exceed the uiview (which by default is 320 x 460).
I've tried setting the view containing the tableview to a height of 900 on viewdidload and setting it to sizeToFit, hoping it would shrink as tablecells are added to the tableview (assuming as cells are added the tableview's frame would scale appropriately). But this doesn't seem to work.
Any thoughts?
Thanks!
I just ran into a similar situation (UIScrollView -> UITableView) and was banging my head against it for hours. The short answer is this: the correct sizes to use as the basis of sizing things out were not available until my ViewController (governing the UIScrollView) was sent viewWillAppear:. Before then, I found that the values were set, but often wrong. (E.g., my first attempt at doing this was in viewDidLoad:, and the sizing values were changed there, but they were wrong!)
So my solution was thus:
In viewDidLoad: cause your enclosed UITableView to reloadData once you have your data in place.
in viewWillAppear: do the following (presuming tableView is the enclosed UITableView and scrollView is the enclosing UIScrollView):
tableView.frame = (CGRect){
tableView.frame.origin,
tableView.contentSize
};
// Finally, set content size based on the last element in the scrollView
// in my case, the last element IS the tableView, yours might be different
scrollView.contentSize =
CGSizeMake(scrollView.width
- scrollView.contentInset.left
- self.scrollView.contentInset.right,
tableView.top
+ tableView.height);
N.B., I was getting some very weird numbers when I checked them before viewWillAppear:; e.g., the tableView.contentSize seemed to be the product of default rowHeight * number of rows, ignoring my custom row heights and excluding any separators! Very odd.
This is just a guess, but since UITableView is a subclass of UIScrollView, can you use the contentSize property to see its overall size after a call to reloadData? You could use the size of the tableview to resize the UIView and the contentSize of the scrollView.
Setup: I have a scrollview with a label, uiimage, and a tableview (grouped) nested within. The label, uiimage, and tableveiw are populated by a webservice. The last section in the grouped table view contains text that will never be the same and could be as long as 5 characters to 250+. Also, this view is pushed from a basic tableview (so it has a navigation bar. If that matters at all).
Expected: The tableview should extend in height depending on the length of the content received from the web service. Then set the scrollview height to accommodate the height of the tableview
Problem: I'm not quite sure how to approach the issue. I really only know how to change the height to fixed values, which will not work properly in many scenarios.
The width and height of the cell are ignored; the cell cell is re-sized according to the value you return from -tableView:heightForRowAtIndexPath: or (failing that) tableView.rowHeight. It might appear as if the cell is big enough if the label in the cell is sized to be big enough, because the label is allowed to be bigger than (and render outside) the cell.
One way is to override -tableView:heightForRowAtIndexPath: to return the correct height. This isn't really the intended use of UITableView (it's primarily designed for lots of rows of a single height, dynamically generated from a list of content).
Another way is to set tableView.tableFooterView = myCustomFooter instead. This is probably the easiest way. Size it correctly before performing the assignment (the height matters; the table view will set the width for you anyway). Also make sure that the autoresizing flags are not set, or the size will appear to randomly change when the table view changes size (e.g. on autorotation).
I'd just focus on the variable height element, figure out the height that fits the text there. To figure out the height that a string will take when rendered use a snippet like this:
CGFloat MY_TABLECELL_WIDTH = 320;
CGFloat MY_MAX_HEIGHT = 10000;
UIFont *MY_FONT = nil; // load the correct font here.
CGSize maxSize = CGSizeMake(MY_TABLECELL_WIDTH,MY_MAX_HEIGHT);
CGSize textSize = [serverString sizeWithFont:MY_FONT
constrainedToSize:maxSize
lineBreakMode:UILineBreakModeWordWrap];
I have a UITableViewCell with two subviews, a UILabel on the left, and a random input control on the right. The random input control on the right can vary in size, as can the length of the text, but since I can set the word wrap of the text on the left, I need to be able to adjust the size of the UILabel based on the width of the random input control. To complicate matters, the app needs to work in both portrait and landscape modes, which give the table cells different widths.
This wouldn't be difficult if I could read the width of the table cells and set the widths of its subviews appropriately, but at creation time the width of the cell is 0.
Any ideas?
Nothing easier than that: every UITableViewCell is also a UIView, which has a method designed for just that: layoutSubviews, which is called whenever the view (here: cell) needs a re-layout. This is where you lay out the content.