Calculating dynamically the height of a custom UITableViewCell - objective-c

I am building a application without the IB and I am writing everything from scratch. I have a custom UITableViewCell and when I try to calculate the height, it behaves like there are no subviews. I could find a lot of discussion about dynamically setting the height, but nothing really about how to calculate it, based on the subviews and how and where to set the constraints. What I did is setting constraints for my first component - UIImageView and add them the contenView of my cell. What I get is 0.
I will appreciate any input on that or at least direction!
heightForRowAtIndexPath:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cell = [[UITableViewCell alloc]init];
cell.imageView.image = [UIImage imageNamed:#"avatar"];
cell.imageView.translatesAutoresizingMaskIntoConstraints=NO;
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|-30-[image]" options:0 metrics:nil views:#{ #"image": cell.imageView }];
[cell.contentView addConstraints:constraints];
NSArray *constraints2 = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-20-[image]" options:0 metrics:nil views:#{ #"image": cell.imageView}];
[cell.contentView addConstraints:constraints2];
[ NSLayoutConstraint constraintWithItem:cell.imageView attribute: NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual toItem:cell.imageView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0.0f];
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
NSLog(#"%f",height);
return height;
}

My solution for this kind of problem is UITableViewAutomaticDimension. You just return this in heightForRowAtIndexPath method. You will have to set tableView.estimatedRowHeight to some value that is close to what you will get.
For instance, if you have 5 cells with height [100,200,250,270,300] you can set estimatedRowHeight to 220 (or something similar).
You can take a look at first answer at Using Auto Layout in UITableView for dynamic cell layouts & variable row heights as well.

I'm not sure that will work, or that it is correct but try this way
static NSInteger someCell = 1;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [UITableViewCell new];
cell.tag = someCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.tag == someCell) return 100.;
return 30.;
}

Related

UIView addSubview nor working in UITableViewCell

barView I have a Table View and, in each cell, I want to draw some statistic bars. I have an empty UIView created in my custom cell, its name is mainView and it is the view where the chart is drawn.
In cellForRowAtIndexPath I'm doing this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomCellIdentifier";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
CGRect barRect = CGRectMake(0, 0, 100, 10);
UIView *barView = [[UIView alloc] initWithFrame:barRect];
[barView setBackgroundColor:[UIColor redColor]];
[cell.mainView addSubview:barView];
return cell;
}
The barView is not displaying on loading. It only shows after I change and return from another tab or after the cell goes out of the screen.
Notes:
I have tried [cell.mainView setNeedsDisplay];
I have a header made with another custom cell.
Try this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomCellIdentifier";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
CGRect barRect = CGRectMake(0, 0, 100, 10);
UIView *barView = [[UIView alloc] initWithFrame:barRect];
[barView setBackgroundColor:[UIColor redColor]];
[cell addSubview:barView]; // use cell.mainView if you have that created somewhere other than here (like IB)
}
return cell;
}
dequeueResuable... can return a nil cell. So you need to have a check, to see if you should alloc/init a new cell. In the code I gave you if dequeueResuable... returns you a proper cell then it would have been created with the subview intact already. So no need to re-add it (hence creating it only in if (!cell))
Your description clearly indicates you are filling your barView outside this function. So that it becomes visible only after transition.
Or you are simply overwriting it (or even overwriting mainview) outside what you have provided here.
Check your viewdidload and viewdidappear and the likes.
I have solved it easily with:
- (void)viewDidAppear:(BOOL)animated
{
[self.tableView reloadData];
}
It seems that adding a subview cant be done before the view is created in the window, before the cells are completely loaded.

UITableViewCell width is greater than UITableView width

I encountered a weird problem. I am creating a custom selected view for my tableviews. However, this selection view doesn't fit well. I found out that this is because my selection view is 300px while the cell itself is 320px. The weird part is that the UITableView's width is actually just 300px only. How will I adjust the cell width of my table?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *tableIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier] autorelease];
if (tableView == bandTable)
{
UIImageView *imageBg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"column1_selected.png"]];
[imageBg setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
[cell setSelectedBackgroundView:imageBg];
[imageBg release];
NSArray *array = [bandDictionary objectForKey:#"Bands"];
cell.textLabel.text = [array objectAtIndex:indexPath.row];
NSLog(#"CELL: %f", cell.bounds.size.width);
}
return cell;
}
Just set
cell.frame = CGRectMake(0,
0,
self.tableView.frame.size.width,
cell.frame.size.height);
in cellForRowAtIndexPath.
Add this two lines after your cell gets created,
[[cell contentView] setFrame:[cell bounds]];
[[cell contentView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
#IvorPrebeg, you shouldn't set the frame for the UITableViewCell inside cellForRowAtIndexPath. The UITableViewCell will automatically be set to the width of the UITableView after the cell did execute layoutSubviews. This will be done automatically after setting a value on the label you inserted in the cell.
Its just important to calculate inside heightForRowAtIndexPath a size based on the font and the text inserted into the cell which is equal to the one inside your cell. In my case it looks like this:
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath{+
ChatMessage *message =
[self.fetchedResultsController objectAtIndexPath:indexPath];
CGSize size = [message.text sizeWithFont:self.messageCell.labelMessage.font
constrainedToSize:CGSizeMake(self.tableView.frame.size.width * 0.8f, HUGE_VAL)
lineBreakMode:NSLineBreakByWordWrapping];
return size.height;
}
And than inside the UITableViewCell:
- (void)layoutSubviews{
[super layoutSubviews];
if( _fInitWidth == 0 ){
self.labelMessage.preferredMaxLayoutWidth =
self.bounds.size.width * 0.8f;
}
[self.contentView setNeedsDisplay];
}
Like you can see, the prefferedMaxLayoutWidth of the UILabel inside my UITableViewCell will be 0.8 of the cells width (320 * 0.8). LayoutSubviews will be excecuted inside cellForRowAtIndex after setting up the cells properties. After that, UITableView will call the heightForRowAtIndexPath and calculate the size for the height. Therefore I calculate the UILabels size by passing a width * 0.8 for constrainedToSize ( tableview width is 320.0 * 0.8 ) the same as in the cell.
The example values are for iPhone devices, they will change on bigger screens of course.
On C#:
public UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath){
UITableViewCell cell = new UITableViewCell();
cell.Frame = new CoreGraphics.CGRect(0, 0,this.selectSchoolTableview.Frame.Width, cell.Frame.Size.Height);
cell.ContentView.AutosizesSubviews = true;
return cell;
}
Another trick:
public UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath){
UITableViewCell cell = new UITableViewCell();
cell.Accessory = UITableViewCellAccessory.None;
cell.ContentView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;
cell.ContentView.AutoresizingMask = UIViewAutoresizing.FlexibleHeight;
return cell;
}

iOS tableview cell handling

I have this code, where "lineaRedToday" is an UIImageView:
- (void)viewDidLoad {
[super viewDidLoad];
lineaRedToday = [UIImage imageNamed:#"line4.png"];}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
MyIdentifier = #"tblCellView";
TableViewCell *cell = (TableViewCell*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"TableViewCell" owner:self options:nil];
cell = tblCell;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[[lineDay objectAtIndex:currentTodaylineRed -1] setImage:lineaRedToday];
return cell;
- (IBAction) change{
lineaRedToday = [UIImage imageNamed:#"linea.png"];
[lineDay setImage:lineaRedToday];
[myTableView reloadData];
}
It is a UITableView with 15 custom UITableViewCell and I want to change the image at an UIImageView, this ImageView is "lineDay", lineDay is present in all cells, and I want change its image at all cells, but the IBAction "change" change the UIImageView only in the last cell and not at all....why?
yes it will change the image in last cell because it has the reference of only last cell, if you want to do this then when you are creating cell in cellForRowAtIndexPath check for a BOOL and set image according that BOOL. Also in IBAction change that BOOL value and call reloadData on table view to reload it. As -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Test";
UITableViewCell *cellView = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cellView == nil)
{
cellView = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UIImageView *bgImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 85.0f)];
[bgImgView setTag:1];
[bgImgView setBackgroundColor:[UIColor clearColor]];
[cellView.contentView addSubview:bgImgView];
[bgImgView release];
}
UIImageView *imgView = (UIImageView*)[cellView viewWithTag:1];
if (buttonPressed)
{
[imgView setImage:[UIImage imageNamed:#"listing_bg"]];
}
else
{
[imgView setImage:[UIImage imageNamed:#"featured_listing_bg"]];
}
return cellView;
}
- (IBAction) change{
buttonPressed = !buttonPressed;
[myTableView reloadData];
}
First, lineaRedToday is an UIImage, not an UIImageView. The first is an image, the second an object in the view hierarchy.
Then, in your code
if(cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"TableViewCell" owner:self options:nil];
cell = tblCell;
}
you use tblCell. I cannot see what this is. You should assign something from what you were just loading from the nib.
I don't know what lineDay is exactly, but a single view (i.e. UIImageView) can only be present in one cell, not in many. If you change the image (lineaRedToday), you still have to set this new image in your views. There should be something like
cell.yourImageView.image = linaRedToday;
when configuring the cell.

Animating a font size change in a UITableView

Hey Simple little app for educational purposes, its a simple uitableview and I want users to be able to use a uislider to adjust the font size as needed. The code I have now works to change the font but only when the view is updated, ie when i pull up or down the table view to reveal other cells. I'd like the font change to be reflected immediately as a user moves the uislider if possible, here's the code that I have working half way:
-(UITableViewCell *) tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier: SimpleTableIdentifier] autorelease];
NSUInteger row = [indexPath row];
cell.text = [listData objectAtIndex:row];
UIImage *image = [UIImage imageNamed:#"star.png"];
cell.font = [UIFont systemFontOfSize:[fontSlider value]];
cell.image = image;
return cell;
}
You could make an IBAction for the fontSlider for the "Value Changed" option as follows:
-(IBAction) refreshResize
{
[self.view setNeedsDisplay];
}
That should work. setNeedsDisplay refreshes the screen.

How to change the height of table view cell

i m trying to read some RSS data. I have the data that is of different sizes. the data is present in tabke view data object. I used the label to add the data and resize the dat. No success. Please help.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
NSLog(#"in the tabel view cell");
heightOfCell=[self tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Default"];
if (cell == nil)
{
//cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(55.0,42.0,245.0,heightOfCell) reuseIdentifier:#"Default"] autorelease];
UILabel *label = [[UILabel alloc] init];
NSString *cellText = [[TableViewData news] valueForKey:[NSString stringWithFormat:#"%d",[indexPath row]]];
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:10.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
label.text = [[TableViewData news] valueForKey:[NSString stringWithFormat:#"%d",[indexPath row]]];
CGSize labelSize = [[[TableViewData news] valueForKey:[NSString stringWithFormat:#"%d",[indexPath row]]] sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
label.lineBreakMode=UILineBreakModeWordWrap;
[label sizeThatFits:labelSize];
cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(55.0,42.0,245.0,heightOfCell) reuseIdentifier:#"Default"] autorelease];
//[label sizeToFit];
[cell addSubview:label];
[label release];
}
}
you need to use the following method.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 45;
}
you need to change the height as per your requirements
It should be noted that the default height is 44.
You need to implement the following method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// some code that compute row's height
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
// This is just Programatic method you can also do that by xib !
}
You can also change it in Interface Builder. Open the *.xib file, select the table, show the Size Inspector, and change the row height. But, please note that the default height is 44 pixels.
There are two methods to set the UITableViewCell's height .:
UITableView has a property named rowHeight, with which you can use to set all the UITableViewCell to the same row height;
If you implement the UITableViewDelegate protocol , and include tableView:heightForRowAtIndexPath: method, it's you responsbility to decide the current UITableViewCell'height. In practice, we always use the delegate method to set the cell's height, only if each cell's height may be dynamic! And the method is preference to the UITableView's rowHeight property!
So, I think you may prefer the second method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellText = [[TableViewData news] valueForKey:[NSString stringWithFormat:#"%d",[indexPath row]]];
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:10.0];
CGSize labelSize = [cellText sizeWithFont:cellFont
constrainedToSize:constraintSize
lineBreakMode:UILineBreakModeWordWrap];
return lableSize ;
}
I saw a lot of solutions but all was wrong or uncomplet. You no need calculate nothing (no font, no size, nothing)...
You can solve all problems with 5 lines in viewDidLoad and autolayout.
This for objetive C:
_tableView.delegate = self;
_tableView.dataSource = self;
self.tableView.estimatedRowHeight = 80;//the estimatedRowHeight but if is more this autoincremented with autolayout
self.tableView.rowHeight = UITableViewAutomaticDimension;
[self.tableView setNeedsLayout];
[self.tableView layoutIfNeeded];
self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0) ;
For swift 2.0:
self.tableView.estimatedRowHeight = 80
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.setNeedsLayout()
self.tableView.layoutIfNeeded()
self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0)
Now create your cell with xib or into tableview in your Storyboard
With this you no need implement nothing more or override. (Don forget number os lines 0) and the bottom label (constrain) downgrade "Content Hugging Priority -- Vertical to 250"
You can donwload the code in the next url:
https://github.com/jposes22/exampleTableCellCustomHeight
References: http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/
You have to know the heights of all cells before the tableView:cellForRowAtIndexPath: delegate call which may require you to store heights inside your view controller or some other list.
The function to customise the heights of cells is tableView:heightForRowAtIndexPath:. The default height for a table view is 44 pixels, for reference.
You can find the documentation here.