UITableCell clipToBounds property odd behavior - ios

I have a custom UITableCell that has labels and a UIView.
Lets say the tablecell is set at a height of 50px and 10 labels are placed with 10px seperation. With cell.clipToBounds = YES, we only see 5 of the labels which is nice and expected.
The custom UIView on this custom table cell is say 200px in height. I ASSUMED the UIView would get clipped as the labels did because the UIView is added as a subview to the cell itself which clips everything else just fine but the UIVIew doesnt get clipped at all. The entire UIView (200px height) is shown.
pseudo code:
cellForRowAtIndexPath... ... {
...
...
CustomCell * cell = ... ...
cell.clipToBounds = YES;
cell.customView.hidden = NO;
[self.view addSubView:cell.customView];
[self.view bringSubviewToFront:cell.customView];
}
In storyboard i literally create a custom cell and add 10 labels as mentioned above and drag and drop a UIView in the cell with a large height.
So why don't views added to "content view" of UITableViewCell get clipped?

also set cell viewContent clipsToBound
cell.contentview.clipToBounds = YES;
and also check if the cells are overlapping, then make sure you are passing the right height in heightForRowAtIndexPath method of tableview.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}

Related

How to change height of UItableView inside UITableViewCell based on number of cells in the UITableView

I have a UITableView inside a UITableViewCell pinned on all four sides. I want to change the height of UITableView based on number of cells it has. For that I have subclassed UITableViewCell (name- TableCell). Inside it, I am using this code to update the tableView height.:
-(void)layoutSubviews{
[super layoutSubviews];
if ([self.data count] <= 5) {
self.tableHeightConstraint.constant = ([self.data count]) * 44.0f;
} else {
self.tableHeightConstraint.constant = 5 * 44.0f;
}
}
I change the height constraint of TableView above dynamically. It works sometimes but other times leads to breaking constraints leading to jittery behaviour of app.
Is this the correct way of changing height?
Please help!
Edit 1:
I am inserting and deleting cells at runtime. The inserted cell has a tableview whose size is dynamic based on number of cells. When I insert (while including the above code) the insertion is correct. But when I delete, the cell (having uitableview) is still visible. When I scroll the cell out of screen, then only it goes invisible.
Kindly help!
if you have tableview inside tableViewCell that is pinned to all sides of the cell (bottom,left,right,top) and it doesn't have a high content hugging priority you can set the height of the cell in the outer tableView in:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
the inner tableview will be stretched since it have pinned constraints

Divide UitableViewCell into two colours

I want to make my table view cell like progress bar . How much progress is done according to that cell's background colour should change . I am not able to do that in UITableViewCell .
Edit
Its not about changing the colour of the alternative cells . Its about two applying two colours into same cell.
Make custom tableview cell class. Add view with 0 or 1 width and required height to cell from interfacebuider to cell's content view or if want to add programmatically then add that view from awakeFromNib of custom cell class method.
Then set desired background color of that view and add increase it's width as per progress.
If there is no specific progress then you can add animation with some duration to increase it's width.
If you are using autolayout then manipulate constraint's outlet to increase width, if not using autolayout then manipulate view's frame to increase width.
Hope this will help :)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *temp= #"CustomCell";
CustomCell *cellheader = (CustomCell *)[tableViewObj dequeueReusableCellWithIdentifier:temp];
// add your if condition
cellheader.contentView.backgroundColor = [UIColor colorWithRed:72.0/255.0 green:72.0/255.0 blue:72.0/255.0 alpha:1.0];
}

seprator is not visible in uitable view for dynamic height?

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

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.

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

Resources