I have a TableView with custom cells. Each cell displays a UIImageView that's 300px large (not all images have the same dimensions).
Then I use a width/height ratio to get the new height dynamically.
At this point, I resize both my UIImageView and the TableViewCell:
// In -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
// Resize my UIIMageView
cell.myImage.frame = CGRectMake(10, 10, 300, newHeight);
// In -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
return newHeight;
But I run into something weird, some of the images expand beyond their cell onto other cells.
Is there something I missed or done wrong?
Thanks for your help.
simply return new height in (UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath" like this
(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 90;
}
Related
I have to make the height of UITableViewCell dynamic with respect to number of images in the cell.
Add following lines of code in your viewDidLoad():
tableView.estimatedRowHeight = 200.0
tableView.rowHeight = UITableViewAutomaticDimension
where estimatedRowHeight is the maximum height you want to set for UITableViewCell.
Also, add this delegate method and return UITableViewAutomaticDimension:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
set the UITableView's rowHight property to UITableViewAutomaticDimension and set the estimated row height to what the most common height is. Then in your layout of the cell make sure there are constraints between all of the visual elements vertically and the cell will be laid out properly. The vertical constraints should be something like:
V:|-(5)-[imageOne]-[imageTwo]-|
Where "|" is the cell's content view.
Implement the delegate method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath (NSIndexPath *)indexPath
#interface yourController : UIViewController<UITableViewDelegate,UITableViewDataSource>
set delegate in view did load
- (void)viewDidLoad {
[super viewDidLoad];
tableview.delegate=self;
tableview.dataSource=self;
}
now method of height of table view cell
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;//your value
}
in cell
- (CGFloat)cellHeight {
[self layoutIfNeeded];
return CGRectGetMaxY(_imageview.frame) + 10;
}
the imageView is the last view in cell. It only needs maxY of the lastView
in controller
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
OJTopicRootCell *cell = (OJTopicRootCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.cellHeight;
}
Use the estimatedHeight methods to quickly calcuate guessed values which will allow for fast load times of the table.
If these methods are implemented, the above -tableView:heightForXXX calls will be deferred until views are ready to be displayed, so more expensive logic can be placed there.
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(7_0);
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
Here is my storyboard:
The method i use to get the image is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
cell.mainImage.file = (PFFile *)object[#"image"];
[cell.mainImage loadInBackground];
}
Row Height is set as:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 501.0f;
}
I also would like to have the Image to be filled perfectly, much like instagram. I dont want images to have bars on the top and bottom.
this was work for me
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
Here's a video of what's going on: https://imgflip.com/gif/kgvcq
Basically, if the cells scroll past the bottom edge of the screen, it won't bounce back. I've tried updating the contentSize of the tableView but that doesn't seem to be the issue. I've also made sure to declare the rowHeight and still no luck. Lastly, I've made sure the bounce properties of the tableView are set properly.
Sorry for not putting up code, here it is:
// data source
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"frame height: %f", tableView.frame.size.height);
NSLog(#"content size height: %f", tableView.contentSize.height);
static NSString *CellIdentifier = #"HabitCell";
HabitTableViewCell *cell = (HabitTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.viewController = self;
cell.delegate = self;
// edit cell
return cell;
}
The NSLogs are returning: 568 and 400 respectively. Would it be the frame causing problems? Also, I have not overridden scrollViewDidScroll.
Implemented Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [self.habits count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath isEqual:_expandIndexPath]) {
return 450 + heightToAdd;
}
return 100;
}
Fixed: I had a call to scrollToRowAtIndexPath in my UIPanGestureRecognizer method. Removed it and it now works perfectly.
I have a table with a custom cell, which has only one UIImageView. I want each cell to have its own height based on an image height I will place in it. In my heightForRow method I have
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
FAppFormula *formula = self.formulas[indexPath.row];
UIImage *img = [formula getFormulaImageSmallSize];
if (img.size.height <= 100){
return 30;
}
return 75;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *cellIDSmall = #"FormulaCellSmall";
FormulaCell *cell = (FormulaCell *)[tableView dequeueReusableCellWithIdentifier:cellIDSmall
forIndexPath:indexPath];
cell.formulaImage.image = formulaImg;
return cell;
}
The cells themselves do get proper heights, however images don't get scaled appropriately. In storyboard cell prototype I set UIImageView's mode to Aspect Fit, however looks like all images get scaled based on a default(?) row height, but not on the one which heightForRowAtIndexPath returns...
Any ideas on why it happens and how to fix it? How it looks now img
I have UITableView, every tableViewCell is custom. Inside my customTableViewCell is a UITextView, TextViews frame is pin or same as its superView which is tableViewCell.
How Am I gonna set the UITableViewCell height dynamically that is proportional to the TextViews size which will also depends to content text that I get from internet.
(sample prototype)
// this is how I manipulate every cell height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// How do I set the height here, whats the best approach for this. with autolayout BTW
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// I initialize every cell here with my ArrayContainer, nothing much to refer here.
}
Follow this answer: Link reference
Add a height constraint to your UITextView and create an outlet to your custom cell class.
Use sizeThatFits: on UITextView to get the size that fits the content and set this value as a constant of the constraint described in 2. Do this in tableView:heightForRowAtIndexPath:.
One caveat is that if there're many cells (hundreds or thousands) then you may hit performance issues.
Primarily get the texts as NSArray and Assign it into the UITextView's as temporary, So that you will get the Height of Cell :
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGRect frame;
// set the height here, According to the NSArray of text
if(textView || section )
UITextView *tempTxtView = [[UITextView alloc] init];
tempTxtView.text = // Add the Text of index according to the Section
// Fit the UITextView to the Content
frame = tempTxtView.frame;
frame.size.height = tempTxtView.contentSize.height;
tempTxtView.frame = frame;
}
return frame.size.height;
}
Once got the UITableViewCell height : Then No need to worry about this, right ?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// I initialize every cell here with my ArrayContainer, nothing much to refer here.
self.textView = [[UItextView alloc] initWithFrame:CGRectMake(cell.frame.origin.x +20, cell.frame.origin.y +10, cell.frame.size.width - 20, cell.frame.size.height - 20)];
// ....... Do further with the NSArray of Text
}
Note : I Haven't Tested, Its just a thought. Hope this will help you
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize titlesize = [TxtValue.text sizeWithFont:[UIFont fontWithName:appdel.strFont size:25.0f] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping]; /// ----- -set width as per your requirement inplace of 300
return titlesize.height+10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}