This question is w.r.t xib and old style of showing custom CellView's in UITableView. I do have autolayouts in my views.
I have a DynamicSubView (dynamic height) of type UIView which I want to reuse in UITableViewCell. For this I have tried 2 things,
Adding DynamicSubview as [cell.contentView addSubview:myDynamicView]
Added a UIView inside cell.xib's contentView with constraint 0-0-0-0 on all Left-Top-Right-Bottom. And a height as NSLayoutConstraint which I am calulating & changing at runtime. And setted identity inspector of that view to DynamicSubview.
In MyCell.m class, I have added created a object of DynamicSubView class in awakeFromNib & in layoutSubView method, I am calculating dynamic height of DynamicSubView.
I tried adding height related code in heightForRowAtIndexPath, but it goes in infinite-loop for the very first time when table loads.
The problem is the cells are getting overlaped. Should I create an array of height values and reload again. Any better solution to reload the cell again with proper height.
Code In MyCell class:
- (void)awakeFromNib
{
[super awakeFromNib];
self.myDynamicView = (DynamicSubView *)self.myContainerView;
[self.myDynamicView setUpView:YES];
}
- (void)layoutSubviews
{
[super layoutSubviews];
[self.myDynamicView doSomething];
self.c_height.constant = [self.myDynamicView getHeightOfDynamicSubview];
//CGRect cellFrame = self.contentView.frame;
//self.contentView.frame = CGRectMake(cellFrame.origin.x, cellFrame.origin.y, cellFrame.size.width, self.c_height.constant);
self.cellHeight = self.c_height.constant;
}
Rather than using the LayoutSubiew() inside the tableViewCell, you can you the following methods in your view controller
SWIFT
self.tableView.estimatedRowHeight = 100 (Let's say minimum height of your tableViewCell is 100)
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
Related
I'm a little confused where should I declare and how to add a subView to a cell to make the cell shorter in width.
I tried to use func viewWillLayoutSubviews(){} in my tableViewCell class but I can't access the tableView from there...weird..
What do I need to subclass for this to work and where do I need to insert this code?
Currently this code just adds a subview to the cell and kind of floats on top of the cell instead of containing the cell. I'm using it in cellForRowAtIndexPath.
let testFrame : CGRect = cell.contentView.frame
var testView : UIView = UIView(frame: testFrame)
testView.backgroundColor = UIColor.redColor()
testView.alpha=0.5
testView.bounds = CGRectMake(cell.bounds.origin.x,
cell.bounds.origin.y,
cell.bounds.size.width - 50,
cell.bounds.size.height);
//TableViewCell.addSubview(testView)
cell.contentView.addSubview(testView)
return cell
A subview is added inside of the cell, you are adding testView as a subview of the cell's content view. The frame of testView will be relative to the cell's content view (testView's superview).
It is not a good idea to change the width of UITableViewCell because there may be unintended side effects. You can override setFrame in a subclass of UITableViewCell which you return from cellForRowAtIndexPath:. More info here: How to set the width of a cell in a UITableView in grouped style
You should look at UICollectionView if you need cells that are not full width or make your table view the width you need.
I create custom cells within my tableview some have images and are tall some are just text. The height of the cells are calculated in heightForRowAtIndexPath, which I beleive is done before cellForRowAtIndexPath is called. I want to place an imageview at the bottom of the cell regardless of heigh, but I am not sure how to get the calculated height from within cellForRowAtIndexPath?
Too late for an answer..
But, like #user216661 pointed out, the problem with taking the height of the Cell or the ContentView is that it returns the cells original height. Incase of rows with Variable height, this is an issue.
A better solution is to get the Rect of the Cell (rectForRowAtIndexPath) and then get the Height from it.
- (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath {
UITableViewCell *aCell = (UITableViewCell *)[iTableView dequeueReusableCellWithIdentifier:aCellIdentifier];
if (aCell == nil) {
CGFloat aHeight = [iTableView rectForRowAtIndexPath:iIndexPath].size.height;
// Use Height as per your requirement.
}
return aCell;
}
You can ask the delegate, but you'll be asking twice since the tableView already asks and sizes the cell accordingly. It's better to find out from the cell itself...
// in cellForRowAtIndexPath:, deque or create UITableViewCell *cell
// this makes the call to heightForRow... and sizes the cell
CGFloat cellHeight = cell.contentView.bounds.size.height;
// alter the imageView y position (assuming the rest of the frame is correct)
CGRect imageFrame = myImageView.frame;
imageFrame.y = cellHeight - imageFrame.size.height; // place the bottom edge against the cell bottom
myImageView.frame = imageFrame;
You are allowed to call heightForRowAtIndexPath yourself! Just pass the indexPath from cellForRowAtIndexPath as an argument and you can know the height of the cell you are setting up.
Assuming you are using a UITableViewController, just use this inside cellForRowAtIndexPath...
float height = [self heightForRowAtIndexPath:indexPath]
I would like to get the size of a UITableView's content view when the table is populated. Any suggestions on how to do this?
// Allows you to perform layout before the drawing cycle happens.
//-layoutIfNeeded forces layout early. So it will correctly return the size.
// Like dreaming before doing.
[tableView layoutIfNeeded];
CGSize tableViewSize=tableView.contentSize;
Here's a utility method that does it the hard way. The negligible advantage is there's no need to call [tableView layoutIfNeeded].
#define CGSizesMaxWidth(sz1, sz2) MAX((sz1).width, (sz2).width)
#define CGSizesAddHeights(sz1, sz2) (sz1).height + (sz2).height
+ (CGSize)sizeForTableView:(UITableView *)tableView {
CGSize tableViewSize = CGSizeMake(0, 0);
NSInteger numberOfSections = [tableView numberOfSections];
for (NSInteger section = 0; section < numberOfSections; section++) {
// Factor in the size of the section header
CGRect rect = [tableView rectForHeaderInSection:section];
tableViewSize = CGSizeMake(CGSizesMaxWidth(tableViewSize, rect.size), CGSizesAddHeights(tableViewSize, rect.size));
// Factor in the size of the section
rect = [tableView rectForSection:section];
tableViewSize = CGSizeMake(CGSizesMaxWidth(tableViewSize, rect.size), CGSizesAddHeights(tableViewSize, rect.size));
// Factor in the size of the footer
rect = [tableView rectForFooterInSection:section];
tableViewSize = CGSizeMake(CGSizesMaxWidth(tableViewSize, rect.size), CGSizesAddHeights(tableViewSize, rect.size));
}
return tableViewSize;
}
For table views whose heights are dynamically sized by their cells' contents--
My tableView is contained in a UIView, whose updateConstraints() function looks like this:
override func updateConstraints() {
self.tableView.layoutIfNeeded()
self.tableViewHeight.constant = min(300, self.tableView.contentSize.height)
super.updateConstraints()
}
tableViewHeight is an IBOutlet to the XIB-assigned height of the table view. I get whichever is smaller--300 points, or the height of the table view's content size.
I don't know how much people will like this answer because I am not sure that I like it. But I managed to get something working with this.
This will work for dynamic height cells. The callback will get called a few times as the tableview figures out its contentView
class ContentSizeNotifyingTableView: UITableView {
var contentSizeDidChange: ((CGSize) -> ())?
override var contentSize: CGSize {
didSet {
self.contentSizeDidChange?(self.contentSize)
}
}
}
How does one obtain the UITableViewCell when within the heightForRowAtIndexPath method, i.e. given the indexPath?
(then I could access the content views I have created to add their heights up)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// How to get the UITableViewCell associated with this indexPath?
}
thanks
EDIT: In fact is there really a valid way to do this? When I put some NSLog statements it seems that heightForRowAtIndexPath it called several times before the calls to cellForRowAtIndexPath (which is where I set up the UILabels in the cell)? This kind implies that I may be tried to use a technique that will not work, i.e. I was hoping in heightForRowAtIndexPath to access the already created labels in each cell to get their heights and add them together for the overall cell row height, HOWEVER if they haven't been set up yet (within cellForRowAtIndexPath) then I guess my approach can't really work?
You can use the tableview's delegate instead of the tableView itself.
id cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
Check the answer here
UPDATED
In new iOS versions and using correct auto constraints you don't need to reference the cell anymore to calculate the cell's height.
Check here
https://www.raywenderlich.com/129059/self-sizing-table-view-cells
This question has no sense because in
heightForRowAtIndexPath
no cells are created yet. Here's how tableView works:
TableView asks it's datasource how many sections it will have. -numberOfSectionsInTableView
Asks it's datasource how many rows each section will have (to know how big scroller should be etc.) -numberOfRowsInSection
Asks it's delegate height of each visible row. To know where cells will be located. - heightForRowAtIndexPath
Lastly it asks datasource to give it a cell to display at given index path -cellForRowAtIndexPath
So you can't access to cells from heightForRowAtIndexPath because with a most likely cell is not created yet.
However in case I misunderstood your question go try:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
The obvious answer is to call cellForRowAtIndexPath, but you may have already discovered there are some issues with that. See this question if you haven't already: UITableView flexible/dynamic heightForRowAtIndexPath
For my last project I used a custom subclass of UICell and implemented a method like this. I then called this from table:heightForRowAtIndexPath: (after looking up the content for that row).
+ (CGFloat) heightOfContent: (NSString *)content
{
CGFloat contentHeight =
[content sizeWithFont: DefaultContentLabelFont
constrainedToSize: CGSizeMake( DefaultCellSize.width, DefaultContentLabelHeight * DefaultContentLabelNumberOfLines )
lineBreakMode: UILineBreakModeWordWrap].height;
return contentHeight + DefaultCellPaddingHeight;
}
i would suggest you to calculate the correct height of a row in table:heightForRowAtIndexPath: by using your data structure (Height of text,images,mutliline text etc..).
And change the component height in their -(void) layoutSubviews method.
If you want to access the cells use tags. But as #Jhaliya has said it is better to calculate the height based on the content of the cell. If your regard is with the position of the cell then I would suggest you to use scroll view and implement the cells yourself wherever you want.
For iOS8:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.estimatedRowHeight = 80
self.tableView.rowHeight = UITableViewAutomaticDimension
}
OR
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
But for iOS7, the key is calculate the height after autolayout,
func calculateHeightForConfiguredSizingCell(cell: GSTableViewCell) -> CGFloat {
cell.setNeedsLayout()
cell.layoutIfNeeded()
let height = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingExpandedSize).height + 1.0
return height
}
Note:
1) If multiple lines labels, don't forget set the numberOfLines to 0.
2) Don't forget label.preferredMaxLayoutWidth = CGRectGetWidth(tableView.bounds)
By default, a UITableViewCell instance positions the label pair textLabel/detailTextLabel in the center of its parent view. I prefer to have the pair aligned to the top of the cell. How do I do that programmatically?
Thanks,
Doug
The docs describe -[UIView layoutSubviews] as
"Overridden by subclasses to layout subviews when layoutIfNeeded is invoked. The default implementation of this method does nothing."
That description is not elaborate, but is accurate. In this case, the method's behavior is to layout your subviews. It will be invoked anytime the device orientation changes. It is also scheduled for later invocation whenever you call -setNeedsLayout.
Since, UIView's implementation does nothing, (and I presume the same for UIControl), you get total creative freedom to make your UIView subclass subviews be positioned wherever you want them.
In subclassing a UITableViewCell, you have a couple of options to try:
Override -layoutSubviews and
manipulate the position of the built-in textLabel and -detailLabel views.
Override -viewDidLoad,
create two of your own UILabels to provide the text and detailed text,
add them to self.contentView, and
override -layoutSubviews to manipulate the position of your custom UILabel views
In a related SO question, the recommendation is to avoid #1, manipulating the built-in textLabel and detailTextLabel.
A more reliable bet would be to do something like this:
#interface MyTableViewCell : UITableViewCell {
UILabel *myTextLabel;
UILabel *myDetailTextLabel;
}
// ... property declarations
#end
#implementation MyTableViewCell
#synthesize myTextLabel, myDetailTextLabel;
- (id) initWithFrame: (CGRect)frame {
self = [super initWithFrame: frame];
if (self) {
myTextLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
[self.contentView addSubview: myTextLabel];
myDetailTextLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
[self.contentView addSubview: myDetailTextLabel];
}
return self;
}
- (void) dealloc {
[myTextLabel release];
[myDetailTextLabel release];
[super dealloc];
}
- (void) layoutSubviews {
// Let the super class UITableViewCell do whatever layout it wants.
// Just don't use the built-in textLabel or detailTextLabel properties
[super layoutSubviews];
// Now do the layout of your custom views
// Let the labels size themselves to accommodate their text
[myTextLabel sizeToFit];
[myDetailTextLabel sizeToFit];
// Position the labels at the top of the table cell
CGRect newFrame = myTextLabel.frame;
newFrame.origin.x = CGRectGetMinX (self.contentView.bounds);
newFrame.origin.y = CGRectGetMinY (self.contentView.bounds);
[myTextLabel setFrame: newFrame];
// Put the detail text label immediately to the right
// w/10 pixel gap between them
newFrame = myDetailTextLabel.frame;
newFrame.origin.x = CGRectGetMaxX (myTextLabel.frame) + 10.;
newFrame.origin.y = CGRectGetMinY (self.contentView.bounds);
[myDetailTextLabel setFrame: newFrame];
}
#end
In MyTableViewCell, you would ignore the built-in text labels and use your own custom UILabels. You take complete control over positioning them within the content rect of the table view cell.
I'm leaving a lot of stuff out. In doing custom layout with text labels, you'd want to consider:
Figuring out your own layout algorithm.
I'm using a layout algorithm above that resizes the custom UILabels to fit their text content, then positions them side-by-side. You'll likely want something more specific to your app.
Keeping the custom labels within the content view.
In -layoutSubviews, you might want logic to keep the custom UILabels sized and positioned so that they don't fall outside the bounds of the content rect. With my naive layout logic, any long text dropped into either UILabel (or both) could cause the labels to be positioned right out of the content view bounds.
How to handle -viewDidLoad/-viewDidUnload.
As coded above, this subclass doesn't handle being loaded from a nib. You might want to use IB to layout your cell, and if you do, you'll need to think about -viewDidLoad/-viewDidUnload/-initWithCoder:
The following method in your UITableViewCell subclass should quickly and concisely align both textLabel and detailTextLabel to the top of the cell (nod to Bill's code), without adding any custom views.
- (void) layoutSubviews
{
[super layoutSubviews];
// Set top of textLabel to top of cell
CGRect newFrame = self.textLabel.frame;
newFrame.origin.y = CGRectGetMinY (self.contentView.bounds);
[self.textLabel setFrame:newFrame];
// Set top of detailTextLabel to bottom of textLabel
newFrame = self.detailTextLabel.frame;
newFrame.origin.y = CGRectGetMaxY (self.textLabel.frame);
[self.detailTextLabel setFrame:newFrame];
}
Layout of the UITableViewCell textLabel and detailTextLabel are not directly modifiable, except by picking one of the defined styles provided by the API.
typedef enum {
UITableViewCellStyleDefault,
UITableViewCellStyleValue1,
UITableViewCellStyleValue2,
UITableViewCellStyleSubtitle
} UITableViewCellStyle;
If you want to customize UITableViewCell layout, you'll need to subclass it and override the -layoutSubviews method.
- (void) layoutSubviews {
// [super layoutSubViews]; // don't invoke super
... do your own layout logic here
}