UITableViewCell circle imageview - ios

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;

Related

UITableViewCell's subviews can't be round

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

Subview of UITableViewcell disappear when UItableViewcell is highlighted,anyway to fix it?

I'm working with RESIDEMENU,and trying to add a line between cells of LeftMenuViewController,here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:21];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];
cell.selectedBackgroundView = [[UIView alloc] init];
}
NSArray *titles = #[#"Home", #"Calendar", #"Profile", #"Settings", #"Log Out"];
NSArray *images = #[#"IconHome", #"IconCalendar", #"IconProfile", #"IconSettings", #"IconEmpty"];
cell.textLabel.text = titles[indexPath.row];
cell.imageView.image = [UIImage imageNamed:images[indexPath.row]];
UIView * lineView= [[UIView alloc]initWithFrame:CGRectMake(10, 0, cell.contentView.bounds.size.width, 3)];
lineView.backgroundColor=[UIColor redColor];
[cell.contentView addSubview:lineView];
return cell;
}
I can see these lines at first, but when I touch any cell, the cell is highlighted ,and the line of this cell disappear weirdly.any way to fix it?
snapshot before :
snapshot when click a cell:
UITableViewCell changes the background color of all sub views when cell is selected or highlighted.
You have three options :
No selection style
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Subclass UITableViewCell and overriding Tableview cell's setSelected:animated and/or setHighlighted:animated
Add the line as a layer
CALayer* layer = [CALayer layer];
layer.frame = CGRectMake(10, 0, cell.contentView.bounds.size.width, 3);
layer.backgroundColor = [UIColor redColor].CGColor;
[cell.contentView.layer addSublayer:layer];
Another workaround is overwriting setBackgroundColor: and expose similar method to change its backgroundColor.
#interface
- (void)setPersistentBackgroundColor:(UIColor*)color;
#implementation
- (void)setPersistentBackgroundColor:(UIColor*)color
{
super.backgroundColor = color;
}
- (void)setBackgroundColor:(UIColor *)color
{
// [super setBackgroundColor:color];
// prohibit from changing its background color.
}

collectionView re-usable cells causing bugs

I am creating a collectionView whose cells are of different sizes and have different content. I am using a cell prototype for these cells, however, when I am adding more than one cell I get weird UI bugs:
This is what it is supposed to look like
This is what it actually looks like
Code:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
Card *card = [[[usermanager getSelectedUser] getCards] objectAtIndex:indexPath.item];
[card setupLayout];
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
if(cell == nil){
cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cardCell" forIndexPath:indexPath];
}
[cell addSubview:card];
cell.clipsToBounds = YES;
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
cell.layer.shadowPath = [[UIBezierPath bezierPathWithRect:cell.bounds] CGPath];
//Add dropshadow
cell.contentView.layer.borderWidth = 1.0f;
cell.contentView.layer.borderColor = [UIColor clearColor].CGColor;
cell.contentView.layer.masksToBounds = YES;
cell.layer.shadowColor = [UIColor blackColor].CGColor;
cell.layer.shadowOffset = CGSizeMake(0, 5.0f);
cell.layer.shadowRadius = 2.0f;
cell.layer.shadowOpacity = 0.5f;
cell.layer.masksToBounds = NO;
cell.layer.borderColor = [UIColor yellowColor].CGColor;
cell.layer.borderWidth = 2.0f;
return cell;
}
It propably has something to do with the fact that I use the reusable cell. Because when I create 2 different prototypes in my storyboard for these cells they have no problems at all. Can anyone help me? Thanks
as you say: your cells will be reused, so if you change any layout or frame or colour these properties will be like you set when the cell will be used the next time. you should subclass UICollectionViewCell and implement the method prepareForReuse where you have to reset all views and properties of the cell to the original values and you have to remove the subview card:
-(void)prepareForReuse {
[super prepareForReuse];
// Reset all, for example backgroundView
self.backgroundView = nil;
}
one more point: why you call UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath]; Thats not correct. You need only UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cardCell" forIndexPath:indexPath];

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];

UITextView in UITableViewCell, autocorrection can't be dismissed

I have a UITextView inside a UITableViewCell. It's working as expected except that I can't dismiss the autocorrection suggestions that pop up. This is what my -tableView:cellForRowAtIndexPath: method looks like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *messageCellIdentifier = #"MessageCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:messageCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:messageCellIdentifier];
}
// Set the background view of the cell to be transparent
UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIImage *ballonImage = [[UIImage imageNamed:#"ChatBubbleGray.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
NSString *text = #"Touch To Type";
// This imageView will be the background of the UITextView
UIImageView *balloon = [[UIImageView alloc] initWithImage:ballonImage];
balloon.frame = CGRectMake(0.0, 0.0, 300, 140);
CGRect textViewFrame;
textViewFrame.origin.x = balloon.frame.origin.x + 10;
textViewFrame.origin.y = balloon.frame.origin.y + 5;
textViewFrame.size.width = balloon.frame.size.width - 12;
textViewFrame.size.height = balloon.frame.size.height - 15;
self.messageTextView = [[UITextView alloc] initWithFrame:textViewFrame];
self.messageTextView.backgroundColor = [UIColor clearColor];
self.messageTextView.returnKeyType = UIReturnKeyDefault;
self.messageTextView.editable = YES;
self.messageTextView.scrollEnabled = YES;
self.messageTextView.autocorrectionType = UITextAutocorrectionTypeYes;
self.messageTextView.autocapitalizationType = UITextAutocapitalizationTypeSentences;
self.messageTextView.delegate = self;
self.messageTextView.text = text;
self.messageTextView.font = [UIFont systemFontOfSize:14.0];
[balloon addSubview:self.messageTextView];
[cell.contentView addSubview:balloon];
return cell;
}
#Peter Warbo: I'm suspecting following:
By default UIImageView's userInteraction property is set to NO. Set it to YES on your ballon image view object.
balloon.userInteractionEnabled = YES;
HTH

Resources