I am using TableviewController. In which i used custom cell having a reply button.When I click on reply button then I want to add view in that same cell. This view is getting added but this cell height is not increasing. how to programatically increase cell height after button click in iOS. I used following code.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row==buttontag1)
{
pppp=[postText objectAtIndex:indexPath.row];
CGSize maxSize = CGSizeMake(280, MAXFLOAT);//set max height
CGSize cellSize = [pppp sizeWithFont:[UIFont systemFontOfSize:14]
constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping];
//this will return correct height for text
return cellSize.height+150;
}
else
{
pppp=[postText objectAtIndex:indexPath.row];
CGSize maxSize = CGSizeMake(280, MAXFLOAT);//set max height
CGSize cellSize = [pppp sizeWithFont:[UIFont systemFontOfSize:14]
constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping];
//this will return correct height for text
return cellSize.height+100;
}
}
-(void)reply_comment:(UIButton*)sender
{
buttontag1=sender.tag;
NSLog(#"I Clicked a button button tag %d",buttontag1);
NSString *child=[replyArray objectAtIndex:sender.tag];
if(child== nil || child == (id)[NSNull null]||[child isEqualToString:#"0"])
{
NSLog(#"no child post");
}
else
{
NSLog(#" child post");
ChildViewController *objChildViewController=[[ChildViewController alloc]initWithNibName:#"ChildViewController" bundle:nil];
[cell.child setHidden:NO];
[objChildViewController getchild:posts_id:buttontag1 :uid:frame2];
[cell.child addSubview:objChildViewController.view];
}
}
Please can anyone help me out??
hope this helps.
#interface ViewController ()
{
int currentselection;
int cellno;
UITableView *table;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// do things with your cell here
// set selection
cellno = indexPath.row;
}
-(void)buttonpressed
{
currentselection = cellno;
[table beginUpdates];
[table endUpdates];
}
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
int rowHeight;
if ([indexPath row] == currentselection) {
rowHeight = 200;
} else rowHeight = 100;
return rowHeight;
}
Related
I have label in a cell, when I click on a cell I want to cell increases as much as the size of label. Currently my code resizes the cell to 240.
My label:
UILabel *cellLabel3 = (UILabel *)[cell viewWithTag:5];
[cellLabel3 setText:[Data objectAtIndex:self.expandedIndexPath.row]];
Code for cell height:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath compare:self.expandedIndexPath] == NSOrderedSame) {
return 240;
}
return 90.0;
}
Try this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.expandedIndexPath = indexPath;
[self.tableView reloadData];
}
This Question has answer already But still posting answer,
Calculate Height of Text from method
CGSize maximumSize = CGSizeMake(kLabelWidth, CGFLOAT_MAX);
CGSize size = [text sizeWithFont:font
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
And replace kLabelWidth with label width & font with your label font
and return size.height from heightForRowAtIndexPath.
I'm creating a messenger app, which displays a dialog between two people.
Parsing API response, I got text for inbox and outbox messages. Then I create a cell, using a UITableViewCell prototype from the storyboard.
All constraints are adjusted correctly.
The problem is that I use
[self.tableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
to scroll the tableView to the bottom to have the last message in focus but the contentView of the upper cells is not counted at this time.
So i had to count height of cell before it is displayed. For this i use
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
RJMessageCell *cell = [self configureBasicCellAtIndexPath:indexPath];
[cell setNeedsLayout];
[cell layoutIfNeeded];
CGSize maximumSize = CGSizeMake(320.0, UILayoutFittingCompressedSize.height);
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:maximumSize].height;
return height;
}
- (RJMessageCell *)configureBasicCellAtIndexPath:(NSIndexPath *)indexPath {
static NSString *inboxIdentifier = #"Inbox";
static NSString *outboxIdentifier = #"Outbox";
NSString *identifier;
RJMessage *message = [[[self.messageSectionsArray objectAtIndex:indexPath.section] messages] objectAtIndex:indexPath.row];
if (message.messageIsMine) {
identifier = outboxIdentifier;
} else {
identifier = inboxIdentifier;
}
RJMessageCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];
cell.messageView.layer.cornerRadius = 10.f;
cell.messageView.clipsToBounds = YES;
if ([message.text isEqualToString:#""]) {
cell.messageTextLabel.text = #" ";
} else {
cell.messageTextLabel.text = message.text;
}
cell.messageTextLabel.numberOfLines = 0;
cell.messageTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.timeLabel.text = [self stringTimeFromTimeInterval:message.messageInterval];
if (message.messageState == RJMessageStateUnread) {
cell.backgroundColor = [UIColor colorWithRed:151/255.0 green:200/255.0 blue:255/255.0 alpha:0.4];
} else {
cell.backgroundColor = [UIColor clearColor];
}
return cell;
}
Also the UILabel on screenshots are custom, to setBounds to label
- (void)setBounds:(CGRect)bounds {
[super setBounds:bounds];
if (self.numberOfLines == 0 && bounds.size.width != self.preferredMaxLayoutWidth) {
self.preferredMaxLayoutWidth = self.bounds.size.width;
[self setNeedsUpdateConstraints];
}
}
And the last thing i do, adding estimatedRowHeight in viewDidLoad
self.tableView.estimatedRowHeight = self.tableView.rowHeight;
self.tableView.rowHeight = UITableViewAutomaticDimension;
The first time they appear everything looks good, but when I scroll the table up and down, my cells change their size randomly.
What's wrong with the cell height?
You have to return Height by calculating the text width & height which you want to place inside the cell.
//Height For Row at index path
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText = [NSString stringWithFormat:#"%#",[arrDataSource[indexPath.row]]];
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:15.0];
NSAttributedString *attributedText =
[[NSAttributedString alloc]
initWithString:cellText
attributes:#
{
NSFontAttributeName: cellFont
}];
CGRect rect = [attributedText boundingRectWithSize:CGSizeMake(tableView.bounds.size.width - 90.0, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
return rect.size.height + 42;
}
I am setting the width and height of UITableViewCell in the delegate methods but the when i check the cell frame in cellForRowAtIndexPath method then its totally different. Below is my Code. I am doing every thing programatically.
self.myTable = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 0.05,self.view.frame.size.height * 0.08,self.view.frame.size.width * 0.90,
(self.view.frame.size.height * 0.90) - tabBarHeight ) style:UITableViewStylePlain];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1.0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row == 0 || indexPath.row == 4){
return self.myTable.frame.size.height * 0.08;
}
return (self.myTable.frame.size.height * 0.28);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
if(indexPath.row != 4){
cell.userInteractionEnabled = NO;
}
switch (indexPath.row) {
case 0:{
NSLog(#"0th cell Frame : %#", NSStringFromCGRect(cell.frame));
break;
}
case 1:{
NSLog(#"1st cell Frame : %#", NSStringFromCGRect(cell.frame));
break;
}
case 2:{
NSLog(#"2nd cell Frame : %#", NSStringFromCGRect(cell.frame));
break;
}
case 3:{
NSLog(#"3rd cell Frame : %#", NSStringFromCGRect(cell.frame));
break;
}
case 4:{
NSLog(#"4rth cell Frame : %#", NSStringFromCGRect(cell.frame));
break;
}
default:
break;
}
return cell;
}
The output for the cell frame i am getting is {{0, 0}, {320, 44}}. But this cell frame is not correct. The Height should be 129 and the width should be something like 288. Can some one guide me what i am doing wrong here?
However, if you still need your given (predefined) size for your cell, you can always use,
For width,
With case of a custom cell, you can make a method like this,
- (CGFloat)cellWidth {
return [UIScreen mainScreen].bounds.size.width;
}
Without custom cell, you can always make a function within the same class where you're creating the UITableView,
- (CGFloat)cellWidth {
return yourTableView.frame.size.width;
}
which will return exact size of your table cell. (I'm always prefer second method because sometime you just don't create device width cells!)
For height,
With case of custom cell, you can make a class method like this,
+(CGFloat)myCellHeight {
return 45.f;
}
Without custom cell, you can always make a function within the same class where you're creating the UITableView,
-(CGFloat)myCellHeight {
return 45.f;
}
which you can use in your UIViewController, with cell class name (for custom cell), [cellClassName myCellHeight] or [self myCellHeight] (default cell in UIViewController) in table's - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath datasource method.
These ways you'll have your cells exact width/height, whenever you want ! :)
Nothing, your code is ok. Your cells will have the size you want. The problem is your cells don't know them final size in this methods cellForRowAtIndexPath. In this methods they think, they have the default size. But before they appear they will be resized. If you define your cells by code, you need use de dimension you expect,this is the tableView width and the height of the methods (heightForCell or estimateHeightForCell). You can check this, by comment this line, and pressing in the delegate: (also you can see).
Line to comment to check:
....
if(indexPath.row != 4){
// cell.userInteractionEnabled = NO;
}
.....
Implement to check:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(#"cell pressed Frame : %#", NSStringFromCGRect(cell.bounds));
}
- (void)viewDidLoad
{
self.myTable = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 0.05,self.view.frame.size.height * 0.08,self.view.frame.size.width * 0.90, // set the frame for your tableview
(self.view.frame.size.height * 0.90) - tabBarHeight ) style:UITableViewStylePlain];
self.myTable.delegate = self;
self.myTable.dataSource = self;
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
my code as below,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:#"Color Cell" forIndexPath:indexPath];
......
......
......
if (UIInterfaceOrientationIsLandscape([[UIDevice currentDevice] orientation])) {
cell.textLabel.font = [UIFont fontWithName:#"HelveticaNeue-Thin" size:14.0f];
} else {
cell.textLabel.font = [UIFont fontWithName:#"HelveticaNeue-Thin" size:24.0f];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat rowHeight;
if (UIInterfaceOrientationIsLandscape([[UIDevice currentDevice] orientation])) {
rowHeight = ([[UIScreen mainScreen] bounds].size.width - 52) / [colorArray count];
} else {
rowHeight = ([[UIScreen mainScreen] bounds].size.height - 64) / [colorArray count];
}
return rowHeight;
}
When running it, table view could auto reorientate correctly, cell height as well. But cell text size does not change accordingly.
Why? How to fix it?
left is on portrait, right is on landscape
UITableViewCell not reload when change orientations in device,reload your table view after changing orientation
For some strange reason the TableView Cells are not fitting the dynamic textual content properly. They should have a slight margin between the top and bottom of cell. Unfortunately, depending on the amount of textual content, some cells have no top and bottom margin(tightly packed) and others have huge margins.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
float result = 30;
if (indexPath.section != 0){
CGSize size = [[self textForIndexPath:indexPath isTitle:NO] sizeWithFont:kITDescriptionFont constrainedToSize:CGSizeMake(220, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
result = MAX(result, size.height);
}
return result;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *doubleCellIdentifier = #"ITDoubleDetailCEllIdentifier";
static NSString *cellIdentifier = #"ITDetailCEllIdentifier";
NSString *cellIdent = (indexPath.section == 0)? doubleCellIdentifier : cellIdentifier;
ITDescriptionCell *cell = (ITDescriptionCell *)[tableView dequeueReusableCellWithIdentifier:cellIdent];
if (!cell) {
UITableViewCellStyle style = (indexPath.section == 0)? UITableViewCellStyleValue2 : UITableViewCellStyleDefault;
cell = [[ITDescriptionCell alloc] initWithStyle:style reuseIdentifier:cellIdent];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.textLabel setNumberOfLines:0];
[cell.textLabel setFont:kITDescriptionFont];
}
else {
[cell.textLabel setText:#""];
[cell.detailTextLabel setText:#""];
}
[cell sizeToFit];
return cell;
}
You need to dynamically calculate the height of your text and based on that and the padding you want, adjust the height of the cell through the following delegate method:
#define PADDING 10.0f
- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath {
Subscription *sub = [_dictTask valueForKeyPath:#"subscription"];
NSArray *meta = sub.meta.allObjects;
NSString *text = [[meta objectAtIndex:indexPath.row] valueForKey:#"sum_value"];
CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING * 3, 1000.0f)];
return textSize.height + PADDING * 3;
}