Can I have cells with different hight in an UITableView? - iphone

I need to present some information in a table view where some information is pretty flat, and other is pretty big. i.e. one information is just a line of text really, while the other is three lines of text.
From what I know, UITableView asks for the hight of all cells. So is there any way to have cells with distinct heights?

Yes, see:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Example implementation:
NSString *string...;
CGFloat CellWidth...;
CGSize size = [string sizeWithFont:[UIFont systemFontOfSize:14.0]
constrainedToSize:CGSizeMake(textWidth, MAXFLOAT)
lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = size.height;
if (height + 13.0) < 44.0) {
return 44.0;
}
return height + 13.0;

I agree with paull, but you could also subclass if you wanted something extra than just resizing the height. Just think about what else you may need. Good luck, cocoa can be a real pain sometimes.

Related

UILabel in UITableViewCell overflows

I have a UILabel inside a UITableViewCell and I was trying to adjust the height, however when the height is greater than the cell height it overflows to the next cell below it. How can I avoid this? I am adding this into my contentView:
[self.contentView addSubview:self.commentsText_];
if you want to hide the overflows.
self.contentView.clipsToBounds = YES;
or you may want to layout by overwriting at
- (void)setNeedsLayout
{
[super setNeedsLayout];
self.commentsText_.frame = .... // layout your label
}
Using following code you can calculate height of label and also change the height of cell
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UILabel *yourlabel;// use your memober class UILabel. I am declare here temporary.
CGSize s = [yourlabel.text sizeWithFont:[UIFont systemFontOfSize:15] // enter your text font size and cell-width
constrainedToSize:CGSizeMake(yourcellwidth, MAXFLOAT) // - 40 For cell padding
lineBreakMode:UILineBreakModeWordWrap];
return s.height; //this will give you height of UILabel view you can change using addition according your requirements
}
Hope, this will help you..

How to resize cell label as per my text length in iphone UITableView?

I have surfed but not working any of the solution as per my requirement
Let me explain first ! I want to resize my label as per my text string ! I have referred some of stackoverflow questions !
Now look I have first string as "Hello First," This string completely fits with cell.
Now the problem with second string. Second string is "Hello Second, How are you ? Everythings all right ? Hows all there ?" This is too long string as per cell content. So I want to make my cell resizable as per string text !!
How can I ?
CGSize maximumSize = CGSizeMake(3000,3000);
UIFont *dateFont = [UIFont fontWithName:#"Arial" size:16];
CGSize dateStringSize = ["YOURString" sizeWithFont:dateFont
constrainedToSize:maximumSize
lineBreakMode:"Yourlable".lineBreakMode];
It will give "Width and height" .As per height you will fix your "heightForRowAtIndexpath" in table view cell(through dynamic)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return dateStringSize.height;
}
(or)else
"set your lable height according to string height"
You need to implement this delegate method heightForRowAtIndexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Load text in NSString which you want in Cell's LabelText.
NSString *cellText=[valueArray objectAtIndex:indexPath.row];
//define font for Labeltext...
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(330.0f, MAXFLOAT);
//sizeWithFont: Returns the size of the string if it were rendered with the specified constraints. So it will break your line according to font size and constraint size.
CGSize labelSize_val = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
return labelSize_val.height+20; // Add 20 in height so your UITableView will be neat and clean.
}
Note:
– sizeWithFont:constrainedToSize:lineBreakMode: is deprecated in iOS 7. Use boundingRectWithSize:options:attributes:context: instead.)
Instead of making dynamic size of your label, you can set it maximum (as in your thought) then set numberOfLines property of label. That will allow and move to next line when it reaches end of label. One more thing to set proper size of your cells. Only you've know the exact length of your data.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
In above function you should calculate the height of the cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Here you should set proper frame and numberOfLines to zero to textLabel to accomodate the long string.

Adjusting row height correctly

Showing dynamic data in table cell make problem for me. I am using
[titleString sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(285,9999) lineBreakMode:UILineBreakModeWordWrap];
function for calculating the size now according to size i need to adjust row sizes. but it is inconsistent, Some time it give size(242,18) and (40,18) (showing width,height). when it gives more width for same height the it breaks in two line text and inconsistency begins. if i take less height then it overflow the text and if less in height then some time left a huge white space.
please help me and suggest some proper way for doing this.
you code seem me correct , there is only one place for modification left in your code that would be use CGFLOAT_MAX instead of 9999.
And Also check the lineBreakMode property, Assign numberOfLines With zero.
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
There is delegate method as follows
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == theRowWhereUwantToIncreaseTheSize)
{
return 105.0f;;
}
}

how to resize UITableViewCell so it fits any detailTextLabel

im making a iphone app so that any text in the detailTextlabel will fit no matter what
for example...if the detailTextLabel is short it will show a normal size cell however it if is long it will show a bigger cell
thank you :D
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGSize maxSize = CGSizeMake(max_size);
CGSize cellSize = [yourString
sizeWithFont:[UIFont systemFontOfSize:15]
constrainedToSize:maxSize
lineBreakMode:UILineBreakModeWordWrap];
return cellSize.height;
}
Consider my implementation:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (theSection) {
case 0:
if (indexPath.row ==6) //the row you need to check for height (if any)
{
if([yourString length]<=35) // if less than 35 chars, just return a basic size of 50
{
return 50;
}
CGSize detailSize = [yourString sizeWithFont:[UIFont systemFontOfSize:20]constrainedToSize:CGSizeMake(270, 4000)lineBreakMode:UILineBreakModeWordWrap];
return detailSize.height;
}
hope it helps, to help your understanding you may want to have a look at 'sizeWithFont' to understand what this is doing to return a size.
You have to use the tableView:heightForRowAtIndexPath: method on your UITableViewDelegate to return the height of the row. Note that this method should be very fast if you have more than a few rows, as it will be called for every row in the table rather than just the visible rows.

Automatic tableView cell size in iPhone?

I want to make a UITableView which automatically adjust it's cell size by it's contents.
The contents are from XML file and the data is just text.
I want to put the text in a cell and it should be automatically does word-wrap and support multi-line.
So each cell can have different height. Is this possible?? or any idea?
Thank you ;)
Yes it is possible. Make use of the following method for doing it,
- (CGFloat)tableView:(UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath {
CGSize cellHeight;
if (indexPath.row == 0) {
cellHeight = [yourXMLContents sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(300.0, 1000.0) lineBreakMode:UILineBreakModeWordWrap];
return cellHeight.height + 20;
} else
return 30;
}
Have you looked at the three20 library? It'll resize the cells to fit the content. In the very least, you can look at how the code is implemented to resize cells.