Table Cell not displaying correctly - ios

I've designed a custom table cell as follows:
However, it is rendering as follows:
as you see, the second label is going in the next cell. Any idea how can I solve this issue and make the cell appear as I designed it?

You need to implement UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
This method:
Asks the delegate for the height to use for a row in a specified
location.
Give a minimum height for each row here. Also if different row have different height according to the content, you have to calculate the height according to the content in that case. Something like this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *stringText=[array objectAtIndex: indexPath.row];
CGSize size = [stringText sizeWithFont:[UIFont fontWithName: "yourFont" size: fontSize] constrainedToSize:maxCGSize];
return labelSize.height + padding;
}

Related

custom cell height from UIImageView size?

I have a UIImageView inside a custom cell. If there is no image to display in cell then cell height should automatically decrease.(ImageView Height should be zero in that case). How to specify constraint for this in xib ?
Your UITableView data source should represent the existence of this image.
You should use the following delegate method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.dataSource[indexPath.rox].imageExists) {
return 50.0; // Change to your value with image
}
return 10.0; // Change to your value without image
}
Your UITableViewDelegate should implement tableView:heightForRowAtIndexPath:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// add your condition here if image exit or not and set cell size dynamically
return [indexPath row] * 20;
}
You will probably want to use NSString's sizeWithFont:constrainedToSize:lineBreakMode: method to calculate your row height rather than just performing some silly math on the indexPath.
Here is good sample code for dynamic size cell.

Dynamic Items iOS

What is the current best practice to have dynamic items in iOS.
As you can see the headline can change in length and height dependant on the string length. I want to shift everything down according to this. It's kind of like Facebook Newsfeed.
What is the best practice? I would prefer using constraints to achieve this.
If you want to add the dynamic content in UITableViewCell, you have to do following things,
1.You should calculate the dynamic Content Size and set UITableViewCell hight
Example:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
float height = [#"" sizeWithFont:[UIFont systemFontOfSize:10.0] constrainedToSize:CGSizeMake(320.0, MAXFLOAT)].height;
return height;
}
2.You should adjust your UI elements frame in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}

Resizing a cell in a TableView

Im trying to make some of my cells bigger in height. i tried using
CGRect rect = cell.frame;
NSLog(#"before height: %f",rect.size.height);
rect.size.height +=20;
cell.frame = rect;
NSLog(#"AFTER height: %f",cell.frame.size.height);
in
cellForRowAtIndexPath
and
willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath
*)indexPath
The log shows that the values have changed but it doesnt show any change in the simulator.
Thanks for the help
use tableView:heightForRowAtIndexPath method.
Apple docs clearly explains what to do. UITableView class reference
Every tableView has a delegate property.Set it to your viewController and implement above method. Its signature is
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
So, based on indexPath, return whatever the height you desire.
If you want constant height for all rows, you can use rowHeight property of UITableView.
Use heightForRowAtIndexPath of UITableViewDelegate. Example:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath.row == _pages.count - 1 ? 408 : 450;
}
To make some cells bigger, you should implement the method tableView:heightForRowAtIndexPath:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (<SOMETHING>) {
return height1;
} else {
return height2;
}
}
tableView:cellForRowAtIndexPath: is for configuring the content of the cell that the tableView needs to display.
Note that tableView:heightForRowAtIndexPath: has performance implications if you have a really large table (1000+ entries), this method is called on every row when the table view displays.

Custom "Floating" UITableViewCells

I have been searching the web for an answer to this and I am sure it has an easy answer.
I am creating an UITableView in my app and I am wanting it to have "floating" table view cells and a menu at the top. Like this:
I am sure that these are custom UITableView Cells, but I am not sure how to create them like this and have them be dynamic in size based on the content and how to include a menu at the top that disappears/shows once the user scrolls down or up.
Any insight on this would be awesome!
This can be done fairly easily with a subclassed UITableViewCell in a grouped table view. The image below shows one I quickly made by dragging in various UI elements, and creating a custom class, which has nothing but IBOutlets in the .h file.
The label with the gibberish in it is tied to the gray view below and to the top of the cell, with no specific height set, so when the cell grows, it will grow. Here is the code I used to populate the table:
- (void)viewDidLoad {
[super viewDidLoad];
self.theData = #[#"One",#"Two",#"Three",#"Four",#"Five",#"Six",#"Seven",#"Eight",#"Nine"];
[self.tableView reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.theData.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *s = #"asdfhfl fl flfh sflhsalfjh fajlhf lf asldf fh asljfafh sjlfh ajf fljf fasjlfhjfhjfhjsf hsjfhsjfhajsfh the end";
CGSize size = [s sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(281, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
return size.height + 130;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RDCell *cell = [tableView dequeueReusableCellWithIdentifier:#"RDCell" forIndexPath:indexPath];
cell.lbl1.text = self.theData[indexPath.section];
cell.lbl2.text = #"asdfhfl fl flfh sflhsalfjh fajlhf lf asldf fh asljfafh sjlfh ajf fljf fasjlfhjfhjfhjsf hsjfhsjfhajsfhajlfjafh";
return cell;
}
Notice that I set the number of sections to the count of the array, so you get separate sections of 1 row each. The code in the heightForRowAtIndexPath is typical of the way you would calculate the cell height (except that you would normally use the index path and get a different string for each cell).
I think thats what you are looking for..
IBScrollViewFloatingHeader

Dynamic table cell height

Using the storyboard, I've created a custom cell for my table view, I've also created a custom class for it with all my properties.
Now, what would be the best way in making the cells height dynamic, where is the best way to do this?
Should I do this in the custom class for the cell? Or in my table view controller?
I imagine it would make more sense to do this in the custom class, but then how should I do this?
Once a specific label is filled in, it should change the height of the cell
You cannot change the height of the cell from your custom drawing class.
You can do this in the viewController that has the UITableView only.
Either by specifying a hardcoded row height for all cells, or by using the
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
and specifying a height for the cells here.
If you want to have different heights for the cells, you should check the indexpath.row property and return the desired height value.
In case you want to change the height of an already drawn in screen cell, you will have to reload that cell to reflect the change using this:
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
Use the following code
-(CGFloat)heightForText:(NSString *)str width:(int)width font:(UIFont *)font lineBreakMode:(NSLineBreakMode) lineBreakMode
{
CGSize textSize;
textSize = [str boundingRectWithSize:CGSizeMake(width, FLT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName : font} context:nil].size;
return textSize.height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == self.yourTable)
{
NSMutableDictionary *dict = [self.yourArray objectAtIndex:indexPath.row] ;
return [self heightForText:[dict valueForKey:#"reviewDescription"] width:300 font:[UIFont fontWithName:kAppRegularFont size:15.0] lineBreakMode:0]+75;
}
return 70;
}
Set your view controller to be the delegate for the table view and then implement the following UITableViewDelegate method in the view controller:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Add these line into your "viewDidLoad" method
self.tableView.estimatedRowHeight = your height here
self.tableView.rowHeight = UITableViewAutomaticDimension;
Don't set default hight into delegate
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
}

Resources