Tableview showing the wrong size of cell [duplicate] - ios

This question already has answers here:
UITableViewCell frame height not matching tableView:heightForRowAtIndexPath:
(3 answers)
Closed 9 years ago.
in table i am setting the height for cell using(heightForRowAtIndexPath)delegate of table view
the code is:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
but i am checking the size of cell in delegate method(cellForRowAtIndexPath) and code is:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Dequeue or create a cell
UITableViewCellStyle style = UITableViewCellStyleDefault;
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:#"BaseCell"];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:style reuseIdentifier:#"BaseCell"] ;
cell.textLabel.text = [NSString stringWithFormat:#"Cell %d", numberOfItems - indexPath.row];
NSLog(#"%f",cell.frame.size.height);
return cell;
}
when i am printing the value(frame of cell)cell on console its giving me 44.00.why this is happening even i am setting height of cell..please explain me and what to do to get the cell oh height 100..thanks in advance
actually i want to make custom type table view which support difrrent orientation of view and it is universal app so it will better to call the cell size in behalf of checking every time (iphone/ipad,diff orintation)....plz help me to accomplish requirement

If the cell is being shown correctly, and by correctly I mean with a height of 100 pixels as you have written in your tableView:heightForRowAtIndexPath:, I'm pretty sure it's because you're asking for the cell's height in the wrong place:
the cell has just been init'd with the default init method, the height returned is therefore the default one, of 44 pixels as nslog prompts in your console, on rendering the delegate sets the right height returned from your method and everything is set up correctly.
I had this issue months ago, for some reasons I needed to know cell's height in the tableView:cellForRowAtIndexPath: method, I came out with a workaround for that: I've stored all rowHeights values in an NSArray, since they were dynamic and different row by row according to their content.
I came out with something like
CGFloat height = [[heightsData objectAtIndex: indexPath.section] objectAtIndex: indexPath.row];

Do you have set your delegate for UITableViewDelegate ?
Try to put any log in your tableView:heightForRowAtIndexPath: method at first to see if your delegate is set.

hi friends dev had given explanation
NSLog(#"%f",[self tableView:tableView heightForRowAtIndexPath:indexPath]);

Related

UILabel size changes when UITableView scroll

I have an old objective c project which has an UITableView. All the UILabels in each cells have been created by code. This is my cellForRowAtIndex
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *metadataListTableIdentifier = #"MetadataListTableViewCell";
MetadataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:metadataListTableIdentifier];
NSMutableDictionary *dict = [theListData objectAtIndex:indexPath.row];
[MetrixListScreenManager setCellDataDictionary:dict];
if (cell == nil || cellReloadCount <= cellReloadLimit) {
cell = [MetrixListScreenManager generateCellForListScreen:self withReuseIdentifier:metadataListTableIdentifier andScreenId:self.screenId];
cellReloadCount++;
}
[SkinApplicationUITableViewController applySkinColorToRelevantControlsForCell:cell];
[MetrixListScreenManager populateCellDataForListScreenId:self.screenId usingCell:cell andDataRow:dict];
return cell;
}
According to this implementation, in generateCellForListScreen method, all the labels are creating by giving fixed height and width. Then in populateCellDataForListScreenId method all the texts are setting for previously created UILabels. Now we want to make to adjust cell height according to the content height. So what I did was in generate cell method I got the text height and replace the predefined height with that. But my problem is when I scroll some UILabel's height getting increased and rest of the UILabels are going beyond the cell bottom margin. What is the proper way to do this? Please help me.
Thanks

How to get cell in heightForRowAtIndexPath?

I have created custom cells in my app.I want to get the each cell in HeightForRowAtIndexPath.Please tell me how can i get the custom cell in this method.I have tried this code but this causes infinite loop & finally crash the app.
HomeCell *cell=(HomeCell *)[tableView cellForRowAtIndexPath:indexPath];
EDIT:
I Have tried this but it gives me cell height as zero.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"HomeCell";
HomeCell *cell = (HomeCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
float tv_view_height=cell.tv_post.frame.size.height;
float like_count_height=cell.label_like_count.frame.size.height;
float first_comment_height=cell.first_comment.frame.size.height;
float second_comment_height=cell.second_cmment.frame.size.height;
float third_comment_height=cell.third_comment.frame.size.height;
Post *user_post=[arr_post objectAtIndex:indexPath.row];
float comment_count=[user_post.comment_count intValue];
if(comment_count<=0)
{
first_comment_height=0;
second_comment_height=0;
third_comment_height=0;
}
else if(comment_count==1)
{
second_comment_height=0;
third_comment_height=0;
}
else if(comment_count==2)
{
third_comment_height=0;
}
float like_count=[user_post.like_count intValue];
if(like_count<=0)
{
like_count_height=0;
}
float total_height=tv_view_height+like_count_height+first_comment_height+second_comment_height+third_comment_height;
NSLog(#"total heigh is %f'",total_height);
return total_height;
}
Please tell which is the best way?
How to get cell in heightForRowAtIndexPath?
It's impossible, because when -heightForRowAtIndexPath is called, no cells are created yet. You need to understand how the UITableView works:
UITableView asks it's datasource how many sections it will have
-numberOfSectionsInTableView
At this point there are no cells created.
UITableView asks it's datasource how many rows each section will have
-numberOfRowsInSection
At this point there are no cells created.
UITableView asks it's delegate height of each visible row, to know where cells will be located
-heightForRowAtIndexPath
At this point there are no cells created.
UITableView asks it's datasource to give it a cell to display at given index path
-cellForRowAtIndexPath
At this point the cell is created.
The height of each cell you can calculate from data model. You don't need the cell – you already know the frame width that will contain a comment, you know it's content, you know it's font, you know linebreak mode, etc. So, you can calculate height. For example:
CGFloat commentsHeight = 0;
Post *user_post = [arr_post objectAtIndex:indexPath.row];
for (NSString *comment in user_post.comments)
{
CGRect commentrect = [comment boundingRectWithSize:CGSizeMake(self.view.bounds.size.width - 18, FLT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin)
attributes:#{NSFontAttributeName:[UIFont systemFontOfSize:15]}
context:nil];
commentsHeight += commentrect.size.height;
}
And you can calculate height of the other components of cell from its data model.
But now, in 2015, it's not the best way. You really would be better to read the tutorials, which showed #Zil, and do it with Autolayout.
You should declare an array for storing TableView cells in cellForRowAtIndexPath and you can use stored cells in heightForRowAtIndexPath. Lets Try using this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"HomeCellID";
HomeCell *cell = (HomeCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[[HomeCell alloc] init] autorelease];
}
// Store table view cells in an array
if (![tableViewCells containsObject:cell]) {
[tableViewCells addObject:cell];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([tableViewCellsArray objectAtIndex:indexPath.row]) {
HomeCell *cell = (HomeCell *)[tableViewCells objectAtIndex:indexPath.row];
// Process your Code
}
return yourCalculatedCellHeight;
}
I would recommend you to take the height form a configuration collection on your viewController.
Something like this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height;
CellConfiguration * selectedCellConfiguration =[_cellConfigurations objectAtIndex:indexPath.row];
switch (selectedCellConfiguration.type) {
case TheTypeYouNeed:
return TheValueYouNeed
default:
height = 44.0f;
break;
}
return height;
}
You could create a new cell from scratch, simply by HomeCell *sexyCell = [[HomeCell alloc]init];
or dequeue one like you did in cellForRow (tableView.dequeueWithReuseIdentifier:).
Though I advise creating one from scratch and disposing it after (setting it to nil), because if you dequeue it there they'll go in queue and cause heavy memory leaks and end up with many cells for the same indexPath.
What you COULD do is the following :
Create a cell with alloc init
Fill it with the real data
use .layoutsubviews on its view
calculate it's size and apply it to your real cell
What you SHOULD do :
Use auto layout and add all the constraints that are necessary, all your labels will size dynamically. It takes about 3 or 4 hours to get the basics of Auto layout, and about a month of regular use to really get the hang of it with ease.
I strongly strongly strongly suggest you do NOT resize using the frame of objects, most labels and views will resize like they should without having to write any code if you use constraints properly.
Once you have done that, because you have cells of varying heights, is using the DynamicHeight property of the tableview and the slight adjustements that comes with it. You can find
A great tutorial here
The same updated tutorial for swift (more up to date but you'd need to translate)
This amazing StackOverflow answer which you MUST read
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:indexpath.row inSection:0];
Custom Cell *cell = [tableview cellForRowAtIndexPath:indexPath];
By this , you will get each cell in your method

Dynamic sized labels inside a table not refreshing in ios

My table has cells which have several labels. I want one of these labels to fit its size so text begins right below the Title (remember that Labels align text vertically unless you fit its container).
Problem is, the very first time the table is loaded all labels' texts are succesfully populated but label sizes don't actually graphically apply until the NEXT time a refresh is asked. (if I ask for a reloadData with the exact same information, the labels' sizes work flawlessly).
This is some of my cellForRowAtIndexPath code:
cell.body.text = user.message;
[cell.body sizeToFit];
The only solution I've found so far is double calling [table reloadData] but this is an ugly solution. Any way I can fix this?
Edit: Previous code was a summary, I'll show the whole code here as requested:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TwitterTweetCell *cell = [tableView dequeueReusableCellWithIdentifier:#"TwitterTweetCell"];
// Populate cell
TweetModelData *tweet = [self.twitterModelData.tweets objectAtIndex:[indexPath item]];
cell.tweetName.text = tweet.user;
cell.tweetChannel.text = tweet.userName;
cell.tweetBody.text = tweet.message;
[cell.tweetBody sizeToFit];
return cell;
}
Regarding cell size, everything is working ok. Depending on the size of the message each cell has a different size which was pre-calculated before.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ((TweetModelData *)[self.twitterModelData.tweets objectAtIndex:[indexPath item]]).tweetHeight + 30.0f;
}
I finally found it. Don't know the reason, but it seems disabling "Use autolayout" on my storyboard fixed it.
I'm guessing auto-layout was overwriting the layout changes I was applying so they had no effect until the next data reload.

UITableView separators placed incorrectly

I am working with a dynamic UITable with prototype cells, which is created in a storyboard in iOS 5. In this table, cell heights depend on the content of the cell and are evaluated programatically in heightForRowAtIndexPath. My issue is that in the table that is displayed cell separator lines are placed at wrong locations. Here are some facts about my program:
Cells contents and highlights are displayed at intended locations
In cellForRowAtIndexPath I use dequeueReusableCellWithIdentifier to create new cells
heightForRowAtIndexPath returns the correct values for all cells
Can anyone please help me figure out this problem? I found a pretty old thread discussing the very same issue a year ago, however no explanations/solutions were suggested (Please see UITableView separators drawn incorrectly through cell reuse?)
Greatly appreciate your help.
Thanks,
Anis
Thank you for replying to my post. This behaviour is weird to me too, since I have had a dynamic cell height table working properly before. Here are The information you requested:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *currentCell = nil;
if([indexPath section] == 0 && [indexPath row]== 0)
{
currentCell = [issueDetailTable dequeueReusableCellWithIdentifier:#"DetailFieldCell"];
NSString *summaryString;
/* Here I call a method to figure out the value for summaryString */
/* Here I call a method to figure out the frame for summaryLabel */
summaryLabel = (UILabel *)[currentCell viewWithTag:1];
[summaryLabel setFrame:summaryRect];
[summaryLabel setText:summaryString];
}
return currentCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height;
if([indexPath section] == 0 && [indexPath row]== 0)
{
NSString *summaryString;
/* Here I call the same method to figure out the value for summaryString */
/* Here I call the same method to figure out the frame for summaryLabel */
/* height = height of the summary label */
}
return height;
}
As far as the storyboard is concerned, I use prototype cells for my table; My table auto resizes and clips subviews and clears graphics context; My cells clear graphics context.
And, I was going to post a screen shot of my table view, but apparently my Stackoverflow reputation points are not sufficient! :|
Thanks again for helping me figure this out.
Best Regards,
Anis

UILabel in a UITableViewCell With Dynamic Height

I'm having some problems implemented dynamic row heights in a UITableView - but it isn't the cells that I'm having a problem with, its the UILabel inside of the cell.
The cell just contains a UILabel to display text. My tableView:heightForRowAtIndexPath: is correctly resizing each cell by calculating the height of the label that will be in it using NSString's sizeWithFont: method.
I have a subclass of UITableViewCell that just holds the UILabel property that is hooked up in storyboard. In storyboard I've set its lines to 0 so it will use as many lines as it needs, and I've set its lineBreak to Word Wrap.
Here is how I'm setting up the cells:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ExpandCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
SomeObject *object = self.tableObjects[index.row];
cell.myLabel.text = [object cellText];
[cell.myLabel sizeToFit];
return cell;
}
When I build this, I get my table view with the cell's all sized to the correct height for their content, but the labels are all 1 line that just runs off the side of the cells. However, if I scroll the table so cell's leave the screen, and then scroll back to them, their label will be resized correctly and the cell will look how I expected it to initially.
I have also attempted calculating the labels frame with the same method I'm calculating the row height with, and I get the same behavior - it doesn't draw correctly until it scrolls off of the screen and back on again.
I have found two ways to work around this, and neither are acceptable solutions.
First, if in viewDidAppear: I call reloadData on my tableview, the cells and labels draw themselves correctly the first time. This won't work for my situation because I will be adding and removing cells to this table, and I don't want to call reloadData every time a cell is added.
The second workaround seems very strange to me - if I leave the font settings at the default System Font 17 on the UILabel, the cells draw themselves correctly. As soon as I change the font size, it reverts to its behavior of not drawing a label correctly until it leaves the screen and comes back, or gets reloadData called on the tableView.
I'd appreciate any help with this one.
I ended up resolving this by alloc/init'ing the label in cellForRowAtIndexPath. I'm not entirely sure why this is a solution - but it appears the problem I was experiencing has to do with how storyboard (or when, perhaps?) creates the objects within the cell. If I alloc/init the label in the cell in cellForRowAtIndexPath, everything loads and sizes correctly.
So... my current fix is to check if the cell has my custom label in it. If it doesn't, I alloc/init the label and put it in the cell. If it does have one, as in its a cell that's been dequeued, then I just set the text in the label that is already there.
Not sure if its the best solution, but its working for now.
I ended up resolving this by unchecking the AutoSizing checkbox in IB. It is unclear why auto-layout was causing this problem.
I ran over the same problem and I end up solving it by calling [cell layoutIfNeeded] before return the cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ExpandCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
SomeObject *object = self.tableObjects[index.row];
cell.myLabel.text = [object cellText];
[cell layoutIfNeeded];
return cell; }

Resources