How to get dynamically height for UITableViewCell - ios

I created Customized tableViewCell, I added UIView SubView in UITableViewCell. So my all dynamic Text & Image Content in UIView and it will change according to text and image size.
but now getting error in HeightforRowAtIndex , here i returned direct UIView frame.size.height+10; it working fine in iOS8.1 but it will not work below all version.
Here below added piece of code & with image.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
_cellbackground=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 310, 0)];
_cellbackground.backgroundColor=[UIColor whiteColor];
[cell addSubview:_cellbackground];
return cell;
}
Here _cellbackground view, height will change dynamically according to text and image content.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return _cellbackground.frame.size.height+10;
}
here I return height according to UIView height, but it will working only on (iPhone 6s)iOS8.1. but others it will not work properly.
Please suggest me best way to get height enter image description here this image from iPhone 5(7.1) , it will not work properly.
enter image description here it is from iPhone 6(8.1) working properly.
Please guide me best solution to return height.( HeightForRowAtIndex).

for dynamically height for UITableViewCell set in viewDidLoad
_tableView.estimatedRowHeight = 100.0;
_tableView.rowHeight = UITableViewAutomaticDimension;
this set automatic cell height as per content height
Let me know if this works...

Related

Autolayout is breaking when UITableViewCell is scrolled especially for UIImageView

I need UITableViewCell in a dynamic height depends on the content length.
This is a prototype cell
And the content shows fine when first loaded like this
But when I scroll the UITableView, the cell's content breaks like this, especially for UIImageView.
In the implementation of the cell, I only used setImage and setText method.
What seems the problem? Or, is there another way to set each cell's height differently after viewDidLoad?
** Edit: this is my implementation of cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NotificationCell *cell = [tableView dequeueReusableCellWithIdentifier:#"notification"];
if(!cell) {
[tableView registerNib:[UINib nibWithNibName:#"NotificationCell" bundle:nil] forCellReuseIdentifier:#"notification"];
cell = [tableView dequeueReusableCellWithIdentifier:#"notification"];
}
[cell setContent:[notifications objectAtIndex:indexPath.row]];
[cell layoutIfNeeded];
return cell;
}
And setContent in NotificationCell.m has only setText and setImage
Because the tableView already has an imageView property and it is on the left side of the cell, you set the image to that imageView. You should rename the outlet of your custom imageView. So, it should be not called imageView, give the other name for it.

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

What is the best way to conditionally display an image created via storyboard?

I'm creating a detail view screen in an app. The content that is being shown in detail may, or may not have an image. I am using estimated row height when displaying my cells.
The xib has the following constraints:
Trailing Space to superview
Leading Space to superview
Bottom Space to superview
Top Space to superview
Height <= 200
This works great when I've got an image displayed, however if there is no image the cell is rendering with a height of about 40 and i'm getting a warning.
Warning once only: Detected a case where constraints ambiguously
suggest a height of zero for a tableview cell's content view. We're
considering the collapse unintentional and using standard height
instead.
What is the best way to resolve this? I really don't want to try to conditionally change the number of rows the table has based on an image attribute.
Ideally I'd like constraints that are such that use the above constraints, image displayed edge to edge, clipping is fine, with a max height of 200. Or if there is no image the height is 0.
Code in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
EntityContentObject *object = (EntityContentObject *)self.resultsArray[0];
self.imageURLString = object.entityPicture;
NSLog(#"IMAGE URL STRING: %#", self.imageURLString);
ImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ideaImgCell"];
if (!cell)
{
[tableView registerNib:[UINib nibWithNibName:#"ImageTableViewCell" bundle:nil] forCellReuseIdentifier:#"ideaImgCell"];
cell = [tableView dequeueReusableCellWithIdentifier:#"ideaImgCell"];
}
if([self.imageURLString isEqualToString:#""]){
// no image to display;
}else{
[cell.ideaImage setImageWithURL:[NSURL URLWithString:self.imageURLString] placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
}
NSLog(#"CELL IDEA IMAGE HEIGHT: %#", NSStringFromCGRect(cell.ideaImage.frame));
return cell;
I think you must have to see this https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html
write these lines in your project
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(no image){ // put condition
return 0;
}
else{
return 200;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
if(no image){
return 0;
}
else{
return UITableViewAutomaticDimension;
}
}

iOS: How to fit UILabel text in UITableViewCell?

This is my first iOS project, so I am learning a lot and have to learn more.
I learnt that in order to fit more items on UITableViewCell, I need to subclass it and then use it. I created TransactionCell and in my ViewController I use it as
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// make last cell visible: http://stackoverflow.com/questions/11928085/uitableview-not-visible-the-last-cell-when-scroll-down
tableView.contentInset = UIEdgeInsetsMake(0, 0, 120, 0);
[tableView registerNib:[UINib nibWithNibName:#"TransactionCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
TransactionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[TransactionCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
TransactionModel *transactionModel = self.transactionsModel.transactions[(NSUInteger) indexPath.row];
cell.dateAndMonth.text = transactionModel.date;
cell.name.text = transactionModel.name;
cell.amount.text = transactionModel.amount;
cell.categoryImage.image = [self getShortImage:[UIImage imageNamed:transactionModel.category]];
cell.transactionTypeImage.image = [self getShortImage:[UIImage imageNamed:#"Debit"]];
cell.imageView.contentMode = UIViewContentModeCenter;
return cell;
}
Also, I set the height of cell as
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
My xib looks like
When I run the project however, I see the following
Things don't fit up!
Question
- How do I make the labels appear completely on TransactionCell?
- How do I fit all the elements in a single row without cutting off
I am sure I need to learn something else, but not sure what
While your label is selected in the xib, go to the autoshrink section in the attributes inspector and change the option to minimum font scale, I usually set it to around 0.5.
Add three separate UILabel inside the cell and set the frame for three different size.
As #Shadowfiend told,you have to work with the auto layout and auto resizing.
Check this link: http://www.appcoda.com/self-sizing-cells/
It is nice tutorial on auto resizing the cells and well described too.
Hope it help.

self.tableView reloadData is not working to vertically center my label text

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCell";
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
Movie *movie = [self.movies objectAtIndex:indexPath.row];
cell.title.text = movie.title;
cell.subtitle.text = movie.subtitle;
cell.subtitle.numberOfLines = 0;
[cell.subtitle sizeToFit];
return cell;
}
I am calling reloadData from two places. One is from the end of a loadInitialData function, which is called from viewDidLoad.
A second one is being called from viewDidAppear, although this is inconsequential to my problem, because it existed before it and exists without it.
I initially load 3 rows of sample data, with titles and subtitles. Now what happens is my subtitle text is vertically centered when this window first launches. If I grab the table and scroll is high up, all of a sudden my [cell.subtitle sizeToFit] goes into action, and my text goes to the top vertically, which is desired.
So my issue is... why is the text vertically centered from the beginning? reloadData doesn't work either. When I return from adding a new row, all rows but the newly added row are vertically aligned to top as they should. The new row is incorrectly vertically centered.
Why doesn't this work? Everything seems good. New data is added etc. Via NSLog statements, I have verified numberOfRowsInSection is immediately called after reloadData is called.
So why does the aligning of the text vertically to the top not work?
Thanks!
This is probably because the UITableViewCell has not yet been layed out and so it does not have a size yet. Try doing the sizeToFit in this UITableViewDelegate method
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
I am not sure this will work, but it worth trying.
Add [cell setNeedsLayout]; before you return the cell so it will layout the cell before presentation.

Resources