increasing the height of cell loading from a custom class? [duplicate] - iphone

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to adjust the hieght of the cell table when loading from the custom cells?
I have an application in which I am loading the cell from different custom class object.It was working fine.But my problem is I am adding a text view on this subclass. I need to adjust the height of the cell in the table view according to the contents of that text view.`
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
return 125;
}
else
{
return 245;
}
}
Now I am doing like this. But I want the cell height to be changed according to the text view content size in the custom class cell. Can anybody help me?

You can do it in easy way just look at this :
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UILabel *label = nil;
cell = [tv dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"Cell"] autorelease];
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];
[[cell contentView] addSubview:label];
}
}
then u can Calculate the Cell Height and here an full example link
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [items objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}

Related

Dynamic UITableViewCell per UILable height with landscape portrait orientation support

I need to create a cell height according to lable's content with orientation support curretly have tried this and working fine for portrait but not in landscape so need solution which works on both so if any one can please help me.
thanks in advance for your efforts.
this is my code in tableview
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *arr = [arrData objectAtIndex:tableView.tag];
NSString *text = [arr objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(contentWidth - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:20] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}
// Customize the appearance of table view cells.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UILabel *label = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
cell.accessoryView = nil;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:NSLineBreakByWordWrapping];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:20]];
[label setTag:1];
[label setBackgroundColor:[UIColor redColor]];
[[cell contentView] addSubview:label];
}
NSArray *arr = [arrData objectAtIndex:tableView.tag];
NSString *text = [arr objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(contentWidth - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:20] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
if (!label)
label = (UILabel*)[cell viewWithTag:1];
[label setText:text];
[label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, contentWidth - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
return cell;
}
In heightForRowAtIndexPath you can set height according to orientation.
when orientation change, reload cells. In heightForRowAtIndexPath use this value to check orientation and while this method is called -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration reload your cells.
So your heightForRowAtIndexPath will be somethign like this
if(UIInterfaceOrientationIsPortrait(orientation)){
}
else
{
}
Make sure you keep your cells subviews as autoresizing flexible.

Table view cell expand or contract with the size of the text

I have been populating a UITable view cell with the data fetched from a database. The length of the data varies for different cells. So i need to expand or Contract the height depend the data length. Presently i am using a custom cell with the following code.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID= #"customcell4fan";
customcell4fan *cell = (customcell4fan *)[tableView dequeueReusableCellWithIdentifier:cellID];
if(cell==nil)
{
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"customcell4fan" owner:nil options:nil];
for(id currentObject in nibObjects)
{
if([currentObject isKindOfClass: [customcell4fan class]])
{
cell = (customcell4fan *)currentObject;
}
}
}
eleme = [xmlElementObjects objectAtIndex:indexPath.section];
NSString *email= [eleme.title stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#" \n\t"]];
NSString *postData= [eleme.description stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#" \n\t"]];
cell.nameLabel.text=email;
cell.postLabel.text=postData;
return cell;
}
try this
http://www.roostersoftstudios.com/2011/04/14/iphone-uitableview-with-animated-expanding-cells/
i find it very helpful
Calculate your cell's height and return in this method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
Calculate the size of the string ,According to assign the height of the cell.
To calculate the height of label use the following code,
CGSize stringSize = [urResponseStr sizeWithFont:[UIFont boldSystemFontOfSize:15]
constrainedToSize:CGSizeMake(320, 9999)
lineBreakMode:UILineBreakModeWordWrap];//stringSize.height is your label height
after calculating height of your two labels assign respective value to the individual cells in the following delegate.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
}
try with this answer
#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 10.0f
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [items objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UILabel *label = nil;
cell = [tv dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"Cell"] autorelease];
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];
[[label layer] setBorderWidth:2.0f];
[[cell contentView] addSubview:label];
}
NSString *text = [items objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
if (!label)
label = (UILabel*)[cell viewWithTag:1];
[label setText:text];
[label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
return cell;
}

dynamically increase height of row -iphone

I am having table view.
I want to dynamically increase the height of the row to some size.
How can I do this.
I know its a silly question.But still can't find the solution.Help me please.
I have used this code.But it is not working :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *lblOutTime = [[UILabel alloc]init];
lblOutTime = [[UILabel alloc] initWithFrame:CGRectMake(2, 20, 200, 40)];
lblOutTime.text =#"Outtime : ";
lblOutTime.backgroundColor = [UIColor clearColor];
lblOutTime.font = [UIFont boldSystemFontOfSize:5.0f];
lblOutTime.font = [UIFont fontWithName:#"HelveticaNeue Heavy" size:5.0f];
[cell.contentView insertSubview:lblOutTime atIndex:1];
return cell;
}
-
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == myIndexPath)
{
return someHeight;
}
return normalHeight;
}
In this case the height is not dynamic. here
is only 2 fixed positions of cell height.
as the if conditions shows.
Here you can change the size of the cell:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.myRow == myCellIndex)
return NEW_HEIGHT;
else return NORMAL_HEIGHT;
}
And you can reload your table whenever you want with:
[myTable reloadData];
add this tableview delegate method to your class.m file.
Make sure your class conforms to UITtableViewDelegate protocol
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == myIndexPath)
{
return someHeight;
}
return normalHeight;
}
If You Want Increase the height of all row use
[tableView setRowHeight:44];
[tableView reloadData];
if You want increase the height of a particular row use UITableView Delegete heightForRowAtIndexPath:
and use this
[tableView reloadData];
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row <[ListOfCartItems count])
{
CGFloat result = 44.0f;
NSString* text = nil;
CGFloat width =220 ;
clsCart *tempCartPro;
tempCartPro=[ListOfCartItems objectAtIndex:indexPath.row];
text=tempCartPro.clsProductItem.productName;
if (text)
{
CGSize textSize = { width, 20000.0f }; // width and height of text area
CGSize size = [text sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
size.height += 90.0f; // top and bottom margin
result = MAX(size.height, result) ; // at least one row
}
return result;
}
else
{
return 230.0;
}
}

dynamic UITableViewCell height

I am trying to get my cell to dynamicly change height depending on how much text is in the cell.. Currently I have the words wrapping.. but as soon as there is to much content for the cell (if it goes to a 3rd line) you cannot see anything past the 2nd line.
This is what I have so far.. hopefully someone can see if I am missing something or doing something wrong... any help would be appreciated.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UILabel *label = nil;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];
[[cell contentView] addSubview:label];
}
// //Display cells with data that has been sorted from startSortingTheArray
NSArray *keys = [self.letterDictionary objectForKey:[self.sectionLetterArray objectAtIndex:indexPath.section]];
NSString *key = [keys objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [key sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
if (!label)
label = (UILabel*)[cell viewWithTag:1];
[label setText:key];
[label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
//Applise current key value to cell text label
//cell.textLabel.text = key;
return cell;
}
//Cell size for word wrapping.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
//Display cells with data that has been sorted from startSortingTheArray
NSArray *keys = [self.letterDictionary objectForKey:[self.sectionLetterArray objectAtIndex:indexPath.section]];
NSString *key = [keys objectAtIndex:indexPath.row];
NSLog(#"%#", key);
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [key sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}
Update the code above is now a working version.. for me anyway :)
Start over and follow this tutorial word for word and you'll be fine:
Cocoa is My Girlfriend
It actually looks like you looked at this tutorial and only followed part of it. I still don't see you setting the frame of the label though.

How can I create a variable sized UITableViewCell?

I can't seem to get the text to actually span multiple lines. The heights look correct. What am I missing?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"StatusCell"] autorelease];
CGRect frame = cell.contentView.bounds;
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
myLabel.text = [[person.updates objectAtIndex:indexPath.row] valueForKey:#"text"];
[cell.contentView addSubview:myLabel];
[myLabel release];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *text = [[person.updates objectAtIndex:indexPath.row] valueForKey:#"text"];
UIFont *font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
CGSize withinSize = CGSizeMake(tableView.frame.size.width, 1000);
CGSize size = [text sizeWithFont:font constrainedToSize:withinSize lineBreakMode:UILineBreakModeWordWrap];
return size.height + 20;
}
Also, what am I missing that makes the labels appear longer than the table cell?
Tweetero provides an example of this in MessageListController.m. The code there renders the following screen:
(Pic is taken from Mashable).
The basic implementation outline:
When constructing a UITableViewCell, create and add a UILabel as a subview in the manner shown in tableviewCellWithReuseIdentifier:. Look for the creation of TEXT_TAG label.
when enriching the UITableViewCell with views, ensure that you format label properly, as is done in configureCell:forIndexPath, similarly look for the label tag TEXT_TAG.
Return the appropriate height for each cell, as is done in tableView:heightForRowAtIndexPath.
myLabel.numberOfLines = 2;
Check the docs for full info on how to use this property.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return theSizeYouWantYourCellToBe;
}