UITableViewCell's subviews can't be round - ios

I want to create a tableview that has some round subview as UIView. I set the UIView's layer.cornerRadius and clipsToBounds. But some views are not round. Who can help me to solve this problem or give me some advice?
My code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * settCellID = #"settingCellID";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:settCellID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:settCellID];
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(100, 10, 20, 20)];
view.layer.cornerRadius = 10;
view.clipsToBounds = 1;
view.layer.rasterizationScale = [UIScreen mainScreen].scale;
view.layer.shouldRasterize = 1;
view.backgroundColor = [UIColor blueColor];
[cell.contentView addSubview:view];
}
return cell;
}
result:

You Will Try it With Create a Custom Cell Class, and create your view not a programmatically its create using storyboard. and set property of view in custom cell class. then implement above code but a little changes here i have mention it.
static NSString * settCellID = #"settingCellID";
'CustomClassName' * cell = (CustomClassName*)[tableView dequeueReusableCellWithIdentifier:settCellID];
cell.viewPropertyName.layer.cornerRadius = 10;
cell.viewPropertyName.clipsToBounds = YES;
cell.viewPropertyName.maskToBounds =YES;
cell.viewPropertyName.backgroundColor = [UIColor blueColor];

The rasterizationScale and shouldRasterize is for "Off-Screen Rendering",but it has no effect on the round view. and the "view.clipsToBounds = YES" is eque to "view.layer.masksToBounds = 1". I already used the CGContextRef and UIBezierPath to create a round view on the UITableViewCell, but they can't create a real round view.

Inside this clipsToBound is Bool so you should Give YES or NO and Try To use maskToBound = YES for Corner Radius If you want to merge the below view Than use clipsToBound accordingly And I don't think that rasterizationScale and shouldRasterize is useful here. I hope This Will Help you.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * settCellID = #"settingCellID";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:settCellID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:settCellID];
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(100, 10, 20, 20)];
view.layer.cornerRadius = 10;
view.clipsToBounds = YES;
view.maskToBounds =YES;
view.layer.rasterizationScale = [UIScreen mainScreen].scale;
view.layer.shouldRasterize = 1;
view.backgroundColor = [UIColor blueColor];
[cell.contentView addSubview:view];
}
return cell;
}
And If You Want To UITableViewCell rounded corners and clip subviews than Follow This Link Click here

Related

Not possible to setup UITableViewCell height

Please, help me to setup UITableViewCell height.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
UILabel *l0, *l1;
if (cell == nil) {
CGRect f = tableView.frame;
float sc0 = pow(dt.PHI_1, 3.f);
float sc1 = 1. - sc0;
CGRect z =CGRectMake(0, 0, 0, 0);//f.size.height
CGRect r =CGRectMake(0, 0, f.size.width, f.size.height / 26.f);
CGRect r0 =CGRectMake(0, 0, f.size.width * sc0, f.size.height / 26.f);
CGRect r1 =CGRectMake(f.size.width * sc0, 0, f.size.width * sc1, f.size.height / 26.f);
cell = [[UITableViewCell alloc] initWithFrame:r]
l0 = [[UILabel alloc] initWithFrame:r0];
l0.tag = 100;
[cell addSubview:l0];
l1 = [[UILabel alloc] initWithFrame:r1];
l1.tag = 101;
[cell addSubview:l1];
[cell.textLabel setFrame:z];
[cell.imageView setFrame:z];
[cell setFrame:r];
[cell.backgroundView setFrame:r];
} else {
l0 = (UILabel *)[self.view viewWithTag:100];
l1 = (UILabel *)[self.view viewWithTag:101];
}
cell.backgroundColor = [[UIColor alloc]initWithWhite:.0f alpha:0.f];
cell.imageView.hidden = YES;
cell.textLabel.hidden = YES;
[l0 setText:#"B"];
l0.backgroundColor = [UIColor yellowColor];
[l1 setText:#"TEXT"];
l1.backgroundColor = [UIColor redColor];
return cell;
}
this code gives this:
method like this did not help me:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGRect f = tableView.frame;
return f.size.height / 26.f;
}
SO, if I setup the height smaller. That is, f.size.height / 26.f I have a transparent bar.
UITableView definition:
table = [[UITableView alloc] initWithFrame:CGRectMake(x,y,w,h)];
table.dataSource = self;
[table setTag:1];
[self.view addSubview:table];
Please, provide me a way to setup cell height. If I set very big height of UITableViewCell it is not modified too - only the top part is shown.
Make custom class for cell and use this class instead of UITableViewCell.
And set your cell's property in - (void)awakeFromNib {} in custom cell class. Try this. this may work for you I think. :)
Better use the custom UITableViewCell with auto layout to create the UI and add these lines in viewDidLoad
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
Thanks to everyone. I found the solution.
Use storeboard to declare UITableView
Use the below code to define the height.
the code:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGRect f = tableView.frame;
return f.size.height / 26.f;
}
I do not understand but it didn't work when I declared the table programmatically.

UITableViewCell circle imageview

I have an UITableView with UITableViewCell and I want to make cell.imageViewas circles. So I did this in my cellForrowAtIndex
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
}
cell.backgroundColor=[UIColor clearColor];
cell.imageView.layer.cornerRadius=cell.imageView.frame.size.width/2;
cell.imageView.layer.masksToBounds=YES;
cell.imageView.layer.borderWidth = 2.0f;
cell.imageView.layer.borderColor=[UIColor whiteColor].CGColor;
But when load the table UIImages are squares and then when I scroll it become circles. How can I solve this problem?
Please help me.
Thanks
Below is the code
[cell.imgview setImage:[UIImage imageWithData:imgData]];
cell.imgview.layer.cornerRadius = cell.img_userpic.frame.size.height /2;
cell.imgview.layer.masksToBounds = YES;
cell.imgview.layer.borderWidth = 0;
Your Imageview's width and height should be same otherwise it won't display circle.
You need to create a custom cell by inheriting UITableViewCell and then configure cell in that class.
Implement awakeFromNib method in it and set your image view as you need in this method.
Use below line of code in your cellForRow Method. For circle you have to set cornerradius of layer and set clipsToBounds property to YES.
cell.imageView.clipsToBounds = YES;
try this code
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(-30, 40, 100, 100)];
imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
imageView.layer.masksToBounds = YES;
imageView.layer.cornerRadius = 50.0;
imageView.layer.shadowRadius=5;
imageView.layer.shadowOpacity=4;
imageView.layer.shadowColor=(__bridge CGColorRef)([UIColor blackColor]);
imageView.layer.borderColor = [UIColor whiteColor].CGColor;
imageView.layer.borderWidth = 3.0f;
imageView.layer.rasterizationScale = [UIScreen mainScreen].scale;
imageView.layer.shouldRasterize = YES;
imageView.clipsToBounds = YES;

Change the border color of view in tableview

I have a UIView in UITableView and I need to change the border color of the UIView in a selected row.
How can I do this?
In UITableView cellForRowIndexPath I declared a UIView named as polygonView.
In UITableView didSelectRowAtIndexPath method I wrote code to change the border color.
I also need my row to be unselected when I select a different row.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
polygonView = [[UIView alloc] initWithFrame: CGRectMake ( 40, 10, 300, 43)];
//polygonView.backgroundColor = [UIColor blackColor];
UIImage *image1=[UIImage imageNamed:#"rectangle_box (2)"];
polygonView.backgroundColor = [UIColor colorWithPatternImage:image1];
polygonView.tag=indexPath.row;
cell.textLabel.text=[names objectAtIndex:indexPath.row];
[cell.contentView addSubview:polygonView];
[cell.contentView addSubview:lbl[indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIColor *color = [UIColor redColor];
polygonView .layer.shadowColor = [color CGColor];
polygonView.layer.shadowRadius = 1.0f;
polygonView .layer.shadowOpacity = 1;
polygonView .layer.shadowOffset = CGSizeZero;
}
There is a didDeselectRowAtIndexPath... delegate method you can implement which gets called when you select a different row. From that delegate method you can access the cell (using cell = tableView.cellForRowAtIndexPath:deselectedIndexPath) and the polygon to change the border color.

How to Implement DataSheet

I want to implement data sheet to display history of user. I want to implement that design like this:
But I dont know how to do that...Can anyone please help me
Edit:
Add the horizontal line at the specific position and the label at the position and it will look like this.
Create a tableview and than in cellForRowAtIndexPath method add this code..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *SimpleTableIdentifier;
UITableViewCell * cell;
SimpleTableIdentifier = #"SimpleTableIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier: nil];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
UILabel * numLbl = [[UILabel alloc] initWithFrame:CGRectMake(0,5,33,30)];
numLbl.text = #"1";
[numLbl setFont:[UIFont fontWithName:#"Helvetica" size:10.0]];
numLbl.backgroundColor = [UIColor clearColor];
[cell addSubview:numLbl];
UILabel * nameLbl = [[UILabel alloc] initWithFrame:CGRectMake(30,5,50,30)];
nameLbl.text = #"john%Lakeview";
[nameLbl setFont:[UIFont fontWithName:#"Helvetica" size:10.0]];
nameLbl.backgroundColor = [UIColor clearColor];
[cell addSubview:nameLbl];
//create a hoizontal separator in cell to display it like column
UIView* hSeparatorview1 = [[UIView alloc] initWithFrame:CGRectMake(25, 0, 1, 30)];
hSeparatorview1.backgroundColor = [UIColor blackColor];
hSeparatorview1.tag = 1;
[cell addSubview:hSeparatorview1];
UIView* hSeparatorview2 = [[UIView alloc] initWithFrame:CGRectMake(85, 0, 1, 30)];
hSeparatorview2.backgroundColor = [UIColor blackColor];
hSeparatorview2.tag = 2;
[cell addSubview:hSeparatorview2];
}
return cell;
}
//this method is used to set the hight of the tableview cell
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
return 30;
}
I have created it for only two label and two horizontal view but you can create as many as you like.
And yes dot forget to place this code in didSelectRowAtIndexPath otherwise horizontal view will disappear when user click the cell.
- (void)tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//get the cell which is selected
UITableViewCell *selectedCell = [atableView cellForRowAtIndexPath:indexPath];
//set cell horizontal saparator view color of selected cell bcoz when cell selected all view color is gone
UIView *hSeparatorview1=[selectedCell viewWithTag:1];
hSeparatorview1.backgroundColor = [UIColor blackColor];
UIView *hSeparatorview2=[selectedCell viewWithTag:2];
hSeparatorview2.backgroundColor = [UIColor blackColor];
}
Better to try with Custom tableview Cell for text,and UIViews for lines.I'm also done the same in my app.

UITableViewCell stretchable backgroundView

I have a UITableView with variable height cells. I would prefer not to have to use a background image, but rather would like to set a backgroundView with the styling that I want. Currently, I can't figure out how to dynamically change the height of my backgroundView based on the height of the cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 300, 60)];
view.backgroundColor = [UIColor whiteColor];
view.layer.cornerRadius = 2.0;
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOffset = CGSizeMake(0, 1);
view.layer.shadowRadius = 0.4;
view.layer.shadowOpacity = 0.2;
[cell.contentView addSubview:view];
[cell.contentView sendSubviewToBack:view];
}
ZSSLog *log = [self.items objectAtIndex:indexPath.row];
cell.textLabel.text = log.logText;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:15.0];
cell.textLabel.textColor = [UIColor grayColor];
return cell;
}
Right now the background views just overlap since they are not being resized:
Is this possible?
Instead of setting the frame of your background view you might want to do something like
UIView *view = [[UIView alloc] initWithFrame:cell.contentView.bounds];
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
This answer assumes you're not using auto-layout since you're setting the frame of your background view. If you're using auto-layout you don't want to be setting the frame at all and instead setting constraints on your background view.
Normally, you'd use the backgroundView property for this. Try setting up your background view like this:
UIView *view = [[UIView alloc] initWithFrame:cell.bounds];
view.frame = UIEdgeInsetsInsetRect(view.frame, UIEdgeInsetsMake(0, 10, 0, 10));
view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
//...other cell config...
cell.backgroundView = view;
But if you really want to put this view inside the contentView, you can do that like:
UIView *view = [[UIView alloc] initWithFrame:cell.contentView.bounds];
view.frame = UIEdgeInsetsInsetRect(view.frame, UIEdgeInsetsMake(0, 10, 0, 10));
view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
//...other cell config...
[cell.contentView addSubview:view];

Resources