How to set the width of a cell in a UITableView in grouped style
A better and cleaner way to achieve this is subclassing UITableViewCell and overriding its -setFrame:
method like this:
- (void)setFrame:(CGRect)frame {
frame.origin.x += inset;
frame.size.width -= 2 * inset;
[super setFrame:frame];
}
Why is it better? Because the other two are worse.
Adjust table view width in
-viewWillAppear:
First of all, this is unreliable, the superview or parent view controller may adjust table view frame further after
-viewWillAppear:
is called. Of course, you can subclass and override-setFrame:
for your UITableView just like what I do here for UITableViewCells. However, subclassing UITableViewCells is a much common, light, and Apple way.Secondly, if your UITableView have backgroundView, you don't want its backgroundView be narrowed down together. Keeping backgroundView width while narrow down UITableView width is not trivial work, not to mention that expanding subviews beyond its superview is not a very elegant thing to do in the first place.
Custom cell rendering to fake a narrower width
To do this, you have to prepare special background images with horizontal margins, and you have to layout subviews of cells yourself to accommodate the margins.
In comparison, if you simply adjust the width of the whole cell, autoresizing will do all the works for you.
Adjust width of Grouped UITableView
Assuming your ViewController
is a UIViewController
, instead of making the table view the main view (which will be resized for you), simply add the table view.
Replace:
self.view = tableView;
with:
[self.view addSubview:tableView];
Now the table view will keep the frame that you set.
Since you want the table view down the left side, and presumably to fill the height, you really should do this:
- (void)viewDidLoad {
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 300, self.view.frame.size.height) style:UITableViewStyleGrouped];
tableView.delegate = self;
tableView.dataSource = self;
NSArray *first = [NSArray arrayWithObjects:@"first", @"second", @"third", nil];
NSArray *second = [NSArray arrayWithObjects:@"fourth", @"fifth", @"sixth", nil];
self.sections = [NSArray arrayWithObjects:first, second, nil];
tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self.view addSubview:tableView];
[tableView reloadData]; // don't reload until it's added and the data is ready
}
Getting width of a cell in a grouped UITableView for setting section header width
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];
CGRect rect = cell.frame;
NSLog(@"Cell width = %f",rect.size.width);
Adjust cell width in grouped UITableView
I think you can't with the grouped style of the UITableView. You could however use the plain style, and create your own view to show as the cell, just like the grouped view.
Set the width of an grouped cell in Xcode
An untidy solution is to make the table view 340 pixels wide, and 10 pixels off the left edge of the screen.
A solution that involves changing properties of private classes is to make a UITableViewCell
subclass, and override its layoutSubviews
method. When I log the subviews, I find these:
">",
">",
">"
What happens if we take those subviews and fill the entire bounds available?
- (void)layoutSubviews;
{
// By default the cell bounds fill the table width, but its subviews (containing the opaque background and cell borders) are drawn with padding.
CGRect bounds = [self bounds];
// Make sure any standard layout happens.
[super layoutSubviews];
// Debugging output.
NSLog(@"Subviews = %@", [self subviews]);
for (UIView *subview in [self subviews])
{
// Override the subview to make it fill the available width.
CGRect frame = [subview frame];
frame.origin.x = bounds.origin.x;
frame.size.width = bounds.size.width;
[subview setFrame:frame];
}
}
At this particular moment, on the iOS 5.1 simulator, this works. Now, some future version of iOS may restructure these classes, causing this method to catastrophically mangle the table layout. Your app could be rejected for changing the properties of UITableViewCellContentView... even though you're only modifying its frame. So, how much do you need to have your cells fill the table width?
change width of tableviewcell swift
The cells in the tableview are supposed to be as wide as their container.
If you need your cells to have a different width than the table view, I would suggest adding a view as subview to cell.contentView
and make that view as wide as you need while making sure the contentView
has clear background and no separator and all (so that it appears it is not there).
Another solution would be to have the tableView
not as wide as it's superview
by adding the left/right padding to it. But the you would have the issue that on the left and right side, where the padding is, you won't be able to scroll the tableView
I consider the cleanest solution to use a collectionView
. It is not that much different than a tableView
and you can configure the entire size of the cell, not just the height.
Hope this helps you fix your problem. Let me know if you need more help.
Set the width of UITableViewCell
Ok, So here's one way of doing this by making a custom cell.
You can set a background color on the TableView to grey.
Then in your custom prototype UITableViewCell
- Set the background color to transparent.
- Drag UIView on it and change its color to White or whatever u like.
- Set the constraints left, right, top and bottom to at least 5 from the view to the cell.
- Make sure you use the delegate methods to set automatic height and then estimated height, if you cell will have dynamic height.
- Also remove the separator in the table view so that there is no line.
- Inside the view you can drop a label to add text.
I think that should serve your purpose, let me know how it goes :)
How to calculate the size of cell in UITableView when using grouped style?
iPhone portrait margins : 10 pixels
iPhone landscape or iPad margins:
- if table width<400 then 10 pixels
- else margin = 6% of table width, with minimum of 31 pixels, max of 45 pixels
UITableView grouped style cell, header and footer default dimensions
doesn't storyboard tell you the section header's and footer's height? It does tell me based on the screenshot below:
Fig 1: Section header and footer height when Cell set to GROUPED
Fig 2: Section header and footer height when Cell set to PLAIN
The section width depends on your application coding. If you are using the UITableViewController
, then the width would be the screen size (depending on iPhone/iPad). If you are having a UITableView
within a UIView
, then your width would be as you sized it.
Also, the cell height (44
) you mentioned is it's default settings. If you are using custom cell, then each cell height could change.
With regard to the cell width, some guy measured it and posted it on his blog. However, if you want to have custom width, then this post on SO will be helpful: How to set the width of a cell in a UITableView in grouped style
Related Topics
How to Launch Safari and Open Url from iOS App
Passing Parameters to Addtarget:Action:Forcontrolevents
How to Enable_Bitcode in Xcode 7
How to Add a Border Just on the Top Side of a Uiview
Passing Arguments to Selector in Swift
How to Detect Whether a User Has an iPhone 6 Plus in Standard or Zoomed Mode
When to Use Dequeuereusablecellwithidentifier VS Dequeuereusablecellwithidentifier: Forindexpath
Swift 3.1 Deprecates Initialize(). How to Achieve the Same Thing
What's the Best Way to Detect When the App Is Entering the Background For My View
Nsurlsession With Nsblockoperation and Queues
Displaying a Stock iOS Notification Banner When Your App Is Open and in the Foreground
Auto Layout Constraints Issue on iOS7 in Uitableviewcell
Converting Escaped Utf8 Characters Back to Their Original Form
How to Navigate from One View Controller to Another Using Swift
How to Get the Name of Image Picked Through Photo Library in Iphone
Ios 6: How to Restrict Some Views to Portrait and Allow Others to Rotate
Swiftui | Using Ondrag and Ondrop to Reorder Items Within One Single Lazygrid