seprator is not visible in uitable view for dynamic height? - ios

I am designing an app in which i have used a table view.This table view uses a custom cell.I have given proportinal height to the table view.T
tableview height constraints:
equal height to mainview
multiplier:189:568
Cell properties
cell height:77
Image constraints:
Label constraints
TextViewContsraints
bottom right label constraints
Code to make the row height dynamic
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:
(NSIndexPath *)indexPath
{
float percentage = (77.0 / 568.0);
float height = self.view.frame.size.height * percentage;
return (height>77.0)?height:77.0;
}
Issue screen

Is the UITableViewCell in IB associated with a UITableViewCell subclass that overrides drawRect:? If so, make sure you are calling the super implementation, as that's what draws the line at the bottom. If you are overriding layoutSubviews make sure no views are obscuring the bottom of the cell.
In my case, i forgot to call [super layoutSubviews] after overriding the layoutSubviews method. Took me hoursto find the problem.
Hope this will help you.

Increase custom cell size to 80-88 may be it solve your problem

Related

Problems With Dynamically Resizing UITableViewCells

I'm attempting to dynamically resize a custom UITableViewCell to fit an attributed string's content. This is the result:
When I initialize my tableView I do the following:
self.chatsTableView.estimatedRowHeight = 72.0;
self.chatsTableView.rowHeight = UITableViewAutomaticDimension;
I also implemented the following method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
As you can see it is resizing my height (the smaller allowed is 72). However, for some reason it's creating a weird effect. I'm using storyboard, and auto layout.
Move it to the - (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath method.
You have to set estimatedRowHeight to UITableViewAutomaticDimension as well.
It should work only with the two properties you set , the row height and the estimated row height.
first try to remove the delegate method.
If the table "Jumps" a little when you scroll (because of a bug) then also implement the two delegate methods, heightForRow and estimatedHeightForRow and return the same values as you set in the properties , estimatedRowHeight = 72 and rowHeight = UITableViewAutomaticDimension.
If it's still doesn't work then I would double check the constraints.
For iOS 8+
Set up cell constraints. Make sure that ALL subviews that affects cell height are vertical connected. In this case: top edge of top label is pinned to superview's top AND top labels bottom edge is pinned to bottom's label top edge AND bottom's label bottom edge is pinned to superview's bottom. Vertical constraints chain have to be closed. In your case it looks like a bottom edge of bottom label is not pinned to contentViews bottom edge.
When dequeuing cell for reuse always use
dequeueReusableCellWithIdentifier:forIndexPath: not a shorter version without indexpath specified.
Set self.tableView.rowHeight = UITableViewAutomaticDimension;
Set self.tableView.estimatedRowHeight = 50.0;. This step is optional but helps table view proper manage things like scroll indicators etc.
Will work :)

UITableViewCell's frame only updated after scrolling

I change the frame of cells (add margins to the left and to the right) according to the code. The problem is cells update their frames only after they disappear and appear again via scrolling. I used table view's - reloadData as well, but it did't help. How do I force cells to be redrawn without scrolling?
- (UITableViewCell *) tableView: (UITableView *) tableView
cellForRowAtIndexPath: (NSIndexPath *) indexPath {
PersonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: #"TableViewCellID"
forIndexPath: indexPath];
cell.frame = CGRectMake(20, cell.frame.origin.y, cell.frame.size.width-2*20, cell.frame.size.height);
/* tried any of those
[cell setNeedsDisplay];
[cell setNeedsLayout];
[cell layoutIfNeeded];
*/
return cell;
}
I'd recommend using .xibs for this. Much easier.
As far as I know, you are not able to change the frame of a cell in that manner. The cell's height is determined by heightForRowAtIndexPath, and the width is set to the width of the tableView.
You may be able to do it in some manner the way you are attempting, but the cleanest way I know is the following.
If you want there to be a margin around the cell, you can:
Create a nib for a UITableViewCell with a UIView containing all your views, and place a border using constraints.
Embed all your content inside a UIView (lets call this borderedContentView) and place this as the immediate subview of contentView
Place constraints relating borderedContentView to the contentView, with the leading, trailing, top and bottom constraints set to the values that create the border width you desire.
If your tableView has a backgroundColor, you'll have to set the contentView's backgroundColor to the same color as the tableView's backgroundColor so as to create the illusion of a margin. Do this in the tableView's delegate method willDisplayCell: or in a subclass of UITableViewCell awakeFromNib or other related method.
Bask in the glory of your margined cells.
You can also do this programmatically if you prefer not to use interface builder, but it is very easy to do in IB.
Hope this helps.
The solution to the problem is to override setFrame method of UITableViewCell. This way it perfectly works.

Custom UIView(.xib) changes frame automatically after adding as subview in UITableViewCell

Due to requirement, I have made a custom UIView with .xib. The problem is when i alloc,init the uiview , the frame is all right but when i add the view as subview to UITableViewCell.contentView, the frame of my custom view dramatically changes. I even tried to enforce the frame after adding as subview but of no help.
I am using storyBoard with autolayout. the problem persists in both case when i set constrains in my custom view and when remove all constrains .
Thanks in advance.
So your issue is that you are not changing height of row when your adding view into your cell's content view. Here is below height method and changes that you need to implement:-
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if((indexPath.row==3 && rowSelected)) //You need to put condition check in which you need to see if that particular row is selected or not.
//Then a flag for checking if view is added or not to it's contentView.
//If both condition true then you need to provide a new height to that particular cell.
return 220; //120 height of your cell + 100 height of your view.
else
return 120;
}

TextView inside a table cell fo tableview not displaying correctly

I am now followind maybe a bit obsolete tutorial from iOS Apprentice regarding geolocation.
Anyway the job to be done seems extremly easy:
placing text view inside table view cell. On storyboard everything looks greate but when I run it, it looks like presented on picture below (the textview covers the below category table cell view item):
I have following settings:
What is the best way to keep textview inside the table view cell and text just wraps, and overllay-y (so it is called in CSS - I am newbie to iOS) will be added to textview?
I agree with #railwayparade you should use
cell.textLabel.numberOfLines = 0;
Use heightForRowAtIndexPath to calculate the size the cell needs to be
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// replace "THE_TEXT" with the text you are displaying and "TEXT_LABEL_FONT" with the font you're using in the cell
CGSize size = ["THE_TEXT" sizeWithFont:"TEXT_LABEL_FONT" constrainedToSize:CGSizeMake(self.table.bounds.size.width, HUGE_VALF) lineBreakMode:NSLineBreakByWordWrapping];
return size.height;
}
You might want to subclass UITableViewCell and override layoutSubviews to set the textLabel to the exact right size, and add padding as you see fit
-(void)layoutSubviews // in your UITableViewCell subclass
{
[super layoutSubviews];
self.textLabel.frame = CGRectMake(0.0f,0.0f,self.contentView.bounds.size.width,self.contentView.bounds.size.height);
}
Don't forget if you do add padding on the width in layout subviews, you'll have to change the constrained width in heightForRowAtIndexPath

objc xcode setting y start position of uitableview cells when there is other content above with flexible size

I have a question about the usage of UITableView. I have added a UIView above the cells of my UITableView (see image).
This is very nice because I can add some images and labels there and it will scroll with the cells of the table view. Now I am calling some REST API to get an image which I want to add in this view above the cells. The problem now is that I dont know the height of the image, so I have to calculate it based on the aspect ratio which already works fine. When I add the image I can change its height correctly and move down labels and buttons BUT the image overlaps some of the visible cells.
My question: How can I move down the frame of the container? of the cells? dynamically based on my image respective View height?
I have tried to set the height of the View in the TableView but it has no effect. So I suppose that I have to set the y start position of the cells but I dont know how.
Do I need to set an y offset in the delegate method -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath ?
Any ideas?
I think the key to this is setting your view to be the table view's tableHeaderView after you change the size of the view. I did it like this in a test app,
-(void)layoutHeader {
self.label.text = #"This is a long text to see if it expands to take up multple lines. The quick red fox jumped over the lazy brown dog.";
[self.label setPreferredMaxLayoutWidth:self.tableView.frame.size.width];
CGRect stringRect = [self.label.text boundingRectWithSize:CGSizeMake(self.tableView.bounds.size.width - 40,CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:self.label.font} context:nil];
CGRect headerFrame = self.header.frame;
headerFrame.size.height = stringRect.size.height + 40;
self.header.frame = headerFrame;
[self.tableView beginUpdates];
self.tableView.tableHeaderView = self.header;
[self.tableView endUpdates];
}
I called this with a delay from viewDidLoad as a test. The beginUpdates, endUpdates code isn't necessary if you don't want to see the rows move down to accommodate the new view size. The property, header, is an IBOutlet to the view I added to the top of the table view in IB, and "label" is a subview of that view.
I would personally just use tableView:viewForHeaderInSection: to build the view out, and in tableView:heightForHeaderInSection: calculate the new height and return that. That way you don't have to worry about bumping things down within the tableView since UITableView will handle the rest for you once you. Just make sure to call [_tableView reloadData]; on your tableView after you get the image.

Resources