I have 2 collections views on one view controller.
I want the cells to resize height & width to the Height of the collection view.
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
float cellHeight = 0;
float cellWidth = 0;
if (collectionView == self.channelsCollectionView) {
cellHeight = self.channelsCollectionView.collectionViewLayout.collectionViewContentSize.height;
cellWidth = cellHeight ;
}
if (collectionView == self.favoritesCollectionView){
cellHeight = self.favoritesCollectionView.collectionViewLayout.collectionViewContentSize .height;
cellWidth = cellHeight ;
}
return CGSizeMake(cellHeight, cellWidth);
}
Try this one
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
float cellHeight = 0;
float cellWidth = 0;
if (collectionView == self.channelsCollectionView) {
cellHeight = self.channelsCollectionView.frame.size.height;
cellWidth = cellHeight ;
}
if (collectionView == self.favoritesCollectionView){
cellHeight = self.favoritesCollectionView.frame.size.height;
cellWidth = cellHeight ;
}
return CGSizeMake(cellHeight, cellWidth);
}
or
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
float cellHeight = collectionView.frame.size.height;
return CGSizeMake(cellHeight, cellHeight);
}
You can manage this by giving collection view tag
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
//here You have to check collection view tag
if(collectionView.tag == 1)
{
//define first collection view height and width
}
else if(collectionView.tag == 2)
{
//define second collection view height and width
}
}
Related
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString * message =[[chatData objectAtIndex:indexPath.row]valueForKey:#"message"];
NSString * message_type = [[chatData objectAtIndex:indexPath.row]valueForKey:#"message_type"];
if([[[chatData objectAtIndex:indexPath.row]valueForKey:#"message_type"] isEqualToString:#"0"]){
CGSize sizeText = [self text:message sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(CGRectGetWidth([UIScreen mainScreen].bounds) - 100, 9999) withLinebreakmode:NSLineBreakByWordWrapping];
CGFloat heightOfRow = sizeText.height + 2 * 7.5f;
return heightOfRow + 50;
}
else
return 225;//message_type 1 3 4
// return self.chatTable.rowHeight;
}
pragma mark - Handling ChatLabel Width
-(CGSize)text:(NSString*)text sizeWithFont:(UIFont*)font constrainedToSize:(CGSize)ConstSize withLinebreakmode :(NSLineBreakMode) linebreakmode
{
CGSize retsize;
if ([text respondsToSelector:#selector(boundingRectWithSize:options:attributes:context:)])
{
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = linebreakmode;
// paragraphStyle.alignment = NSTextAlignmentLeft;
NSDictionary * attributes = #{NSFontAttributeName : font,NSParagraphStyleAttributeName : paragraphStyle};
retsize = [text boundingRectWithSize:ConstSize options:NSStringDrawingUsesFontLeading
|NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
retsize.height =ceilf(retsize.height);
retsize.width =ceilf(retsize.width);
NSLog(#"%f",retsize.width);
}else{
if ([text respondsToSelector:#selector(sizeWithFont:constrainedToSize:lineBreakMode:)]){
retsize = [text sizeWithFont:font
constrainedToSize:ConstSize
lineBreakMode:linebreakmode];
retsize = [text boundingRectWithSize:ConstSize options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil].size;
}
}
//[self viewWillAppear:YES];
return retsize;
}
Try this code.
1.if you use this code,you should give only top,bottom,left and right constraint for label.
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
//Here you give a minimum height of the cell size.
return 110;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
How interleave value form Items per Row using PBJHexagon Class Flow Layout on my custom UICollectionView?
hi guys I'm Using iOS hexagon grid layout for UICollectionViews PBJHEXAGON
if you look the file .h is like:
#import <UIKit/UIKit.h>
// iOS hexagon grid layout for UICollectionViews
#interface PBJHexagonFlowLayout : UICollectionViewFlowLayout
#property (nonatomic) NSInteger itemsPerRow;
#end
file.m :
#import "PBJHexagonFlowLayout.h"
CG_INLINE CGFloat CGFloat_nearbyint(CGFloat cgfloat) {
#if defined(__LP64__) && __LP64__
return nearbyint(cgfloat);
#else
return nearbyintf(cgfloat);
#endif
}
CG_INLINE CGFloat CGFloat_floor(CGFloat cgfloat) {
#if defined(__LP64__) && __LP64__
return floor(cgfloat);
#else
return floorf(cgfloat);
#endif
}
#interface PBJHexagonFlowLayout ()
{
NSInteger _itemsPerRow;
}
#end
#implementation PBJHexagonFlowLayout
#synthesize itemsPerRow = _itemsPerRow;
#pragma mark - UICollectionViewLayout Subclass hooks
- (void)prepareLayout
{
[super prepareLayout];
if (_itemsPerRow == 0)
_itemsPerRow = 4;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray *layoutAttributes = [[NSMutableArray alloc] init];
NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:0];
for (NSInteger i = 0 ; i < numberOfItems; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
[layoutAttributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
}
return layoutAttributes;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger row = (NSInteger) CGFloat_nearbyint( CGFloat_floor(indexPath.row / _itemsPerRow) );
NSInteger col = indexPath.row % _itemsPerRow;
CGFloat horiOffset = ((row % 2) != 0) ? 0 : self.itemSize.width * 0.5f;
CGFloat vertOffset = 0;
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
attributes.size = self.itemSize;
attributes.center = CGPointMake( ( (col * self.itemSize.width) + (0.5f * self.itemSize.width) + horiOffset),
( ( (row * 0.75f) * self.itemSize.height) + (0.5f * self.itemSize.height) + vertOffset) );
return attributes;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return NO;
}
- (CGSize)collectionViewContentSize
{
NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:0];
CGFloat contentWidth = self.collectionView.bounds.size.width;
CGFloat contentHeight = ( ((numberOfItems / _itemsPerRow) * 0.75f) * self.itemSize.height) + (0.5f + self.itemSize.height);
CGSize contentSize = CGSizeMake(contentWidth, contentHeight);
return contentSize;
}
now in my ViewController I have:
ViewController.h :
#import <UIKit/UIKit.h>
#import "PBJHexagonFlowLayout.h"
#import "customCell.h"
#interface ViewController : UIViewController<UICollectionViewDelegate,UICollectionViewDataSource>
{
IBOutlet UICollectionView *collectionView;
}
#end
ViewController.m :
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
PBJHexagonFlowLayout *flowLayout = [[PBJHexagonFlowLayout alloc] init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
flowLayout.sectionInset = UIEdgeInsetsMake(3, 3, 3, 3);
flowLayout.minimumInteritemSpacing = 3;
flowLayout.minimumLineSpacing = 3;
flowLayout.headerReferenceSize = CGSizeZero;
flowLayout.footerReferenceSize = CGSizeZero;
flowLayout.itemSize = CGSizeMake(100.0f, 115.0f);
flowLayout.itemsPerRow = 3; // I NEED 3 and 2 Dinamically!! HELP!!
[collectionView setCollectionViewLayout:flowLayout];
//------------------------------------------//
// THANKS GUYS //
// GRETTINGS FROM BOLIVIA //
// ROCK ON!!!! n_n' //
//------------------------------------------//
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
# pragma mark - Collection Functions
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 18;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"Cell_Bolivia";
customCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.hexagonImage.image = [UIImage imageNamed:#"orangeHexagon.png"];
return cell;
}
#end
I did try to do the test randomly in:
flowLayout.itemsPerRow = [self getRandomValueBetween:2 and:3];
with:
-(int) getRandomValueBetween:(int)lowerBound and:(int)upperBound
{
int rndValue = lowerBound + arc4random() % (upperBound - lowerBound);
return rndValue;
}
but the PBJHexagon Class only show in static way:
How interleave value for Items per Row as shown in the example:
thanks a lot guys!!!
1.Just make the collection view frame wider and move left with half width of the cell.
Then you can set the first and the last cell in the short row with an empty cell.
Here is sample code in Swift
override func viewDidLoad() {
super.viewDidLoad()
//...
flowLayout.itemSize = CGSizeMake(80.0, 92.0)
flowLayout.itemsPerRow = 6 // only 4 item will displayed fully, the first and the last cells will only show half
self.collectionView!.setCollectionViewLayout(flowLayout, animated: false)
self.collectionView!.frame = CGRectMake(-40, 0, self.view.frame.width+80, self.view.frame.height) // move the frame to left with 80/2 and modify the width.
//....
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("hexagon", forIndexPath: indexPath) as! HexagonCell
// set a null image if is the first or last one in short row
// ..............
return cell
}
I have a static UITableViewCell that contains a UITextView (that dynamically gets resized based on the content it has,) and a UIView.
The cell's height dynamically changes based on the height of the textView and the view.
Here is my code:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
[self textViewFitToContent:textView];
[self.tableView beginUpdates];
[self.tableView endUpdates];
[self scrollToLocation:textView];
return YES;
}
- (void)textViewFitToContent:(UITextView *)textView
{
textView.scrollEnabled = YES;
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
textView.frame = newFrame;
}
- (void)scrollToLocation: (UITextView *)textView
{
UIView *parentView = textView.superview;
while (![parentView isKindOfClass:[UITableViewCell class]]) {
parentView = parentView.superview;
}
NSIndexPath *indexPath = [myTableView indexPathForCell:(UITableViewCell *)parentView];
[myTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
int padding = 48;
if (indexPath.section == 0 && indexPath.row == 0)
{
return 163;
}
else if (indexPath.section == 1 && indexPath.row == 0) {
return textView1.frame.size.height + self.view1.frame.size.height + padding;
}
else if (indexPath.section == 1 && indexPath.row == 1) {
return textView2.frame.size.height + self.view2.frame.size.height + padding;
}
else if (indexPath.section == 1 && indexPath.row == 2) {
return textView3.frame.size.height + self.view3.frame.size.height + padding;
}
else if (indexPath.section == 1 && indexPath.row == 3){
return textView4.frame.size.height + self.view4.frame.size.height + padding;
}
return 44;
}
Problem
The problem is, when I type in the first textView (it happens to every letter I type,) all other textView's height becomes smaller. If I type in any other textViews, (not the first one,) the first textViews height becomes bigger, and all other textViews height becomes smaller.
Give each textView a tag. If they're in TableViewCells...make the indexPath.row the tag for each textView. In your UITextViewDelegate methods. Use an IF ELSE and do something like
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (textView.tag == 1) {
[self textViewFitToContent:textView];
[self.tableView beginUpdates];
[self.tableView endUpdates];
[self scrollToLocation:textView];
}
if (textView.tag == 2) {
[self textViewFitToContent:textView];
[self.tableView beginUpdates];
[self.tableView endUpdates];
[self scrollToLocation:textView];
}
if (textView.tag == 3) {
[self textViewFitToContent:textView];
[self.tableView beginUpdates];
[self.tableView endUpdates];
[self scrollToLocation:textView];
}
return YES;
}
There is a NSTextField in a NSTableView.
The height of the text in the window resizing is increased.
but Not grow the cells in a table view.
In this case, the height of table view cell to alter how do you like?
in short, When resizing the size of the text view, I want to change the size of the cell.
like this: (Mac store)
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row;
{
KFCustomCellView *cellView = [tableView makeViewWithIdentifier:#"MainCell" owner:self];
NSString *comment = [[_commentList objectAtIndex:row]valueForKey:#"COMMENT"];
[cellView.comment setStringValue:comment];
float cellheight = [comment heightForStringDrawing:comment font:[cellView.comment font] width:self.view.frame.size.width * 0.8];
if (cellheight > cellView.comment.frame.size.height)
{
return (cellheight + 65);
}
return 90;
}
Use following approach the to resize cell height according to textview content :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *DataCellId = #"DataCell";
dataCell = (Cell *)[tableView dequeueReusableCellWithIdentifier:DataCellId];
if(!dataCell)
{
dataCell = [[[Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DataCellId] autorelease];
}
dataCell.dataCellTextView.tag = indexPath.row;
dataCell.dataCellTextView.text =[yourDataArry objectAtIndex:indexPath.row];
return dataCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
#define PaddingForTableRowHeight 24
NSString *text;
if([yourDataArry count] > indexPath.row)
{
text = [yourDataArry objectAtIndex:indexPath.row];
} else {
text = #"";
}
UIFont *dataFont = [UIFont boldSystemFontOfSize:16.0f];
CGSize textSize ;
if([UICommonUtils checkIfiOS7])
{
// For iOS7
textSize = [text boundingRectWithSize:CGSizeMake("YOUR CELL WIDTH", MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:dataFont} context:nil].size;
} else {
textSize = [text sizeWithFont:dataFont constrainedToSize:CGSizeMake("YOUR CELL WIDTH" - PaddingForTableRowHeight,MAXFLOAT)];
}
float textViewHeight = MAX(kDefaultCellHeight, textSize.height+PaddingForTableRowHeight);
return textViewHeight;
}
-(void)textViewDidBeginEditing:(UITextView *)textView
{
[self scrollToCursorForTextView:textView];
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
if([yourDataArry count] > textView.tag)
{
[yourDataArry replaceObjectAtIndex:textView.tag withObject:textView.text];
}
}
- (void)textViewDidChange:(UITextView *)textView
{
dataUpdated =YES;
if([yourDataArry count] > textView.tag)
{
[yourDataArry replaceObjectAtIndex:textView.tag withObject:textView.text];
}
[self performSelector:#selector(dataReload) withObject:nil afterDelay:0.0];
[self scrollToCursorForTextView:textView];
}
-(void)dataReload
{
[self.tbl beginUpdates];
[self.tbl endUpdates];
}
There are two methods which are used to draw a cell if you know
1-viewForTableColumn
2-objectValueForTableColumn
In the above methods,when you draw your textfield you can give the dynamic height or your row as you are giving in heightforrowatindexpath method.By doing this your cell will get the accurate height. If you have any problem in calculating dynamic height then tell me I'll post the code here for calculating it.
There is a custom cell. In it there is two labels. One of them called nameLabel's height has to change dynamically. It can be 1,2 or 3 lines sometimes. But the rows are on eachother, they cross their own row lines. How can I solve this problem?
The labels and Custom Cell object's Use Autolayout option is disabled.
The label height has to change dynamic, And then the CustomCell's. And then tableRow. My head is confused. And why can't I see CustomCell's background color is changing?
Thanks
Here is my code for :
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel *label = [[UILabel alloc] init];
label.numberOfLines = 0; // allows label to have as many lines as needed
label.text = [[self.dataList objectAtIndex:indexPath.row] objectForKey:#"NAME"];
CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(320, 63) lineBreakMode:NSLineBreakByWordWrapping];
CGFloat h = labelSize.height;
NSInteger x=0.0;
if (h==63.0) x=30;
if (h==42.0) x=20;
if (h==21.0) x=10;
return h+30;
}
- (UITableViewCell*)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = #"cellid";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil)
{
cell = (CustomCell*)[[[NSBundle mainBundle]
loadNibNamed:#"CustomCell" owner:nil options:nil]
lastObject];
}
// customization
NSDictionary *d = [self.dataList objectAtIndex:indexPath.row];
cell.nameLabel.text = [d objectForKey:#"NAME"];
cell.cityLabel.text = [d objectForKey:#"CODE"];
cell.indexPath = indexPath;
return cell;
}
At this link there is picture of simulator and xib of the Custom Cell to understand easy the problem:
http://compfreek.wordpress.com/2013/08/14/custom-cell-for-table-row-height-changes-dynamic/
Hear is another way try this change according to ur code it may different, but it may solve your problem.I am putting all views through code only, because it is easy for me check this out.
//in your subclassed class
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
//may be u did same i am adding the label to cell
UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectZero];
nameLabel.numberOfLines = 5; //set number of lines
nameLabel.tag = 100;
[self addSubview:nameLabel];
UILabel *otherLabel = [[UILabel alloc] initWithFrame:CGRectZero];
otherLabel.tag = 200;
[self addSubview:otherLabel];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
//do your customisation when cell is selected
}
-(void)layoutSubviews
{
//in this method i am setting the frames for label
[super layoutSubviews];
UILabel *nameLabel = (UILabel *) [self viewWithTag:100];
nameLabel.text = self.nameString;
CGSize maxSize = CGSizeMake(self.bounds.size.width / 2, MAXFLOAT);//set max height
CGSize nameSize = [self.nameString sizeWithFont:[UIFont systemFontOfSize:17]
constrainedToSize:maxSize
lineBreakMode:NSLineBreakByWordWrapping];
nameLabel.frame = CGRectMake(self.bounds.origin.x +2,self.bounds.origin.y+3, nameSize.width, nameSize.height);
UILabel *otherLabel = (UILabel *) [self viewWithTag:200];
otherLabel.frame = CGRectMake(nameLabel.frame.size.width+15,self.bounds.origin.y+3, 100, 40);
otherLabel.text = self.otherString;
}
//in your main class
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
if(cell == nil)
{
cell = [[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CustomCell"]autorelease];
}
//change it for ur need
NSString *name = #"hear is your long length name uzamaki naruto from uzamaki clan";
NSString *other = #"other name";
cell.nameString = name;
cell.otherString = other;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *name = #"hear is your long length name uzamaki naruto from uzamaki clan";
//replace it your height logic
float cellWidth = 350; // ur cell width
CGSize maxSize = CGSizeMake( cellWidth / 2, MAXFLOAT);//set max height
CGSize nameSize = [name sizeWithFont:[UIFont systemFontOfSize:17]
constrainedToSize:maxSize
lineBreakMode:NSLineBreakByWordWrapping];
return nameSize.height + 10;// for ur height
}