minimumLineSpacing for some cells in UICollectionView - ios

I'm trying to change minimumLineSpacing for some cells depending on what row they are.
Imagine having 10 cells; I want minimumLineSpacing to be 8 for all of them except for cell number 7.
I tried doing it via delegate:
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
But I just wish that it would pass back a NSIndexPath instead of a section.
I'd appreciate any input.

One workaround:
If you have it subclassed , you should be able to do this very easy with autolayout and a XIB.
Example:
If you have a constraint in the cell to the bottom of the contentView, you can simply set the constraint to an NSLayoutConstraint property and update the constant of the constraint for that specific cell only to give an extra spacing.
To keep the same proportions and height as the rest of the cells, you only add the extra spacing (same as you added to the constant) to the itemSize.height to that specific cell.
If you don't have this subclassed with autolayout or anything. You can do it programatically the same way but with a subView frame CGRect

Related

UI Tableview cell not expanding when text is added at the runtime

I am using table view for my chat application, in which the chat responses are added either one of two textviews as shown in the image. The thing is whenever I add a large text, the cell height does not expand to show the full text. It only shows the last line of that text. I am using objective-c. Is there a way to solve this ?. I do not want to completely create a new view like the questions asked before, I want changes to this specific view.
First, you need to use Label and set top and bottom constraint between label and contentView cell.
In order to allow the self-sizing cell mechanism to work, you must set the rowHeight property on the table view to the constant UITableViewAutomaticDimension. Then, you simply need to enable row height estimation by setting the table view's estimatedRowHeight property to a nonzero value, for example:
_tableView.estimatedRowHeight = 44.0; // set to whatever your "average" cell height is
_tableView.rowHeight = UITableViewAutomaticDimension;
You need to defined self-sizing cell like this.
tableCountries.estimatedRowHeight = 100.0;
tableCountries.rowHeight = UITableViewAutomaticDimension;
Make sure you have a proper constraint. You can have a constraint like this.
In the below label you need to set your textView top position. That's why when the below label expands it automatically expand label and text view will push to bottom.
I think your cell is not gettings its height because of textview scrolling maybe that will help:-
Use UITableViewAutomaticDimension and for that your tableview cell content should justify both top-bottom leading and trailing constraints to calculate cell height.
Set scrollEnabled to "false" for your textview.
With everything on the place your cell will get its height for sure.
you can do it in many ways :
1) You can set dynamic height of cell using UITableViewAutomaticDimension.
You need to set proper constraints in storyboard like setting top and bottom.
in ViewDidLoad add below :
tblView.rowHeight = UITableViewAutomaticDimension
tblView.estimatedRowHeight = 100
and in tableView cell's height method :
return UITableViewAutomaticDimension
2) Find text's height dynamically using below function :
let width = 100.0 \\your text label's width you gave
var height = chatMsg.text?.height(withConstrainedWidth: width, font: chatMsg.font!)
using above code you can find the height of your text. and you can use it dynamically
3) there are many code available to use for chat application in which you just need to pass text/image, and it will handle everything else.
Link : https://github.com/kerry/iMessageBubble
Add these two delegate methods:
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
Create Custom UITableViewCell with Three UILabel like lbl1,lbl2,lbl3.
Add the AutoLayout Constraints to lbl1 left, right, top and bottom. With fixed height (30).
Then set same constraints to remaining two Labels.
Here is main things:
1.select lbl1 heigh and double click on height constraint and set >=30
Do same things with lbl2 and lbl3.
Final step is:
Select lbl2 top constraints, change the constraints property like <=8.
Do same things with lbl3, that's it we are done all.
thanks..

Update constraint in UITableVeiwCell

I have a view in UITableViewCell , height of view can be 90 or 60 depending upon condition. I made a IBOutlet to height constraint and updating its value in cellForRow method , but getting layout waring in logs. Do I need to call any method like layoutIfNeeded in cellForRow after updating the constant ?
UITableViewDelegate has this method, that determines the height of cell.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
Implement this method and depending on you condition return the required height.
Apart from this you need to make sure that your constraints don't break, when supplied with this height.
So here are the steps,
Drag drop a UITableViewCell from 'Object Library'.
Select the cell and from 'Size Inspector', set the 'Row Height' to 60.
Set you subview and their contstraints. Don't put a height constraints on the 'Content View' of the table view cell.
Implement the above mentioned method and return height as 60.
If this same cell can have height 90, set 90 in step two, four and make sure constraints don't break when you set 90 in step two.

Self-Sizing TableView Cells based on two subview's height

TableView Description
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 60.0
Cell Description
I have a cell with 2 Labels(Title, Description) inside a ContainerView and 1 ImageView as below. Cell height will vary based on Description Label’s content.
Contraints of all views
There are two cases that I should handle
ContainerView.height greater than (ImageView.height + ImageView.Top + ImageView.Bottom). Here cell's height will be based on ContainerView.height
ContainerView height less than (ImageView.height + ImageView.Top + ImageView.Bottom). Here I expect Cell should consider (ImageView.height + ImageView.Top + ImageView.Bottom) as the height and make ContainerView vertically centre to Cell.
Expected Result in both the cases
Problem
If I set constraints for 1st case then 2nd case is not working and vice versa (I’m aware that by removing ContrainerView.Top, Bottom and making it Vertically Centre to SuperView case 2 result can be achieved)
Is there a way to achieve expected result in both the cases by using same set of IB constraints and UITableViewAutomaticDimension?
Give fixed height and width to the image view . Otherwise let the tableViewCell know the top and bottom of the imageview. So that it can calculate the correct cell height
First make sure that you are using self-sizing cells:
https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html
Make Top and Bottom constraints for both image view and the container view to the edges of the cell and make them >=.
Alternatively, you could try Horizontal Stack View and make rigid (highest priorities) constraints to each edge of the cell.
Use these delegates,
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100; // height of default cell
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
Edit
Try this one.
Your view hierarchy should be like this
ImageView Constraints
Add a view and put both labels into it.
Label Container contraints
Label Title constraint
Label Description constraint
Edit Output

Build up UICollectionView from center

I'm facing a problem where I want to realize a UICollectionView that essentially builds up from the center.
Let's say I have this view which shows a list of people:
When the user keeps adding multiple persons using the +, I want the CollectionView to expand but stay in the center of the view:
How do I realize this behavior?
If you are not using autolayout and size classes then you can set "center" is a collection view like this...
[collectionView setCenter:self.view.center];
And if autolayout is enabled and you are using it then do this...
Set CenterX and CenterY constrains of collection view
Set Height and width constraints of collection view
Instead of a custom flow layout, an alternative approach would be to have a full height collection view as normal and then set a top and bottom inset to visually center the cells:
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
let inset = (collectionView.frame.size.height - ((COUNT_OF_CELLS / CELLS_PER_ROW) * HEIGHT_OF_CELL) / 2)
return UIEdgeInsetsMake(inset, 0, inset, 0);
}
You will need to keep track of the total number of cells, number of cells on a row, and the cells height for the calculation to work right.

seprator is not visible in uitable view for dynamic height?

I am designing an app in which i have used a table view.This table view uses a custom cell.I have given proportinal height to the table view.T
tableview height constraints:
equal height to mainview
multiplier:189:568
Cell properties
cell height:77
Image constraints:
Label constraints
TextViewContsraints
bottom right label constraints
Code to make the row height dynamic
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:
(NSIndexPath *)indexPath
{
float percentage = (77.0 / 568.0);
float height = self.view.frame.size.height * percentage;
return (height>77.0)?height:77.0;
}
Issue screen
Is the UITableViewCell in IB associated with a UITableViewCell subclass that overrides drawRect:? If so, make sure you are calling the super implementation, as that's what draws the line at the bottom. If you are overriding layoutSubviews make sure no views are obscuring the bottom of the cell.
In my case, i forgot to call [super layoutSubviews] after overriding the layoutSubviews method. Took me hoursto find the problem.
Hope this will help you.
Increase custom cell size to 80-88 may be it solve your problem

Resources