Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS UITableViewCell cell.imageView setting rounded corner

Hey all I am trying to set cell.imageView's cornerRadius, but it doesn't seem to work.

cell.imageView.layer.cornerRadius=9;

Will it work or should I add a custom UIImageView in my cell to have rounded corners?

I also tried this

cell.imageView.layer.borderWidth=2;
cell.imageView.layer.borderColor=[[UIColor blackColor]CGColor];

But it also doesn't seem to work. Has anybody faced a similar issue?

like image 742
Satheesh Avatar asked Apr 19 '13 10:04

Satheesh


1 Answers

First add Framework -> QuartzCore.framework to your project
then import header file in your ViewController.m file

#import <QuartzCore/CALayer.h>

Add following code in your cellForRowAtIndexPath method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *cellIdentifier = @"TableViewCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

        // Rounded Rect for cell image
        CALayer *cellImageLayer = cell.imageView.layer;
        [cellImageLayer setCornerRadius:9];
        [cellImageLayer setMasksToBounds:YES];
    }                                

    cell.imageView.image = [UIImage imageNamed:@"image_name.png"];

    return cell;
}
like image 106
Navnath Godse Avatar answered Oct 09 '22 05:10

Navnath Godse