2

I made custom uiTableViewCell and used it UITableview like screen shot. Problem is first and last cell's round corner on the groupd table. enter image description here

How to make fist and last cell round corner

dobiho
  • 1,025
  • 1
  • 11
  • 22
  • possible duplicate of [How to customize the background/border colors of a grouped table view?](http://stackoverflow.com/questions/400965/how-to-customize-the-background-border-colors-of-a-grouped-table-view) – rckoenes Oct 31 '11 at 14:46
  • Please do a thorough search on StackOverflow before posting queries. There are so many questions like this. – Legolas Oct 31 '11 at 14:48
  • 1
    I have read it , but I don't solve it for my problem which happens from UITableViewCell and groups table. – dobiho Nov 02 '11 at 05:59
  • Try this http://stackoverflow.com/questions/3122657/uitableviewcell-has-square-corner-with-image – Tony Pham Sep 12 '12 at 07:40

3 Answers3

3

Add tableView as a property to your cell, and set it in your controller:

@property (nonatomic, weak) UITableView *tableView;

Then you can implement it like this:

- (void)layoutSubviews {
    [super layoutSubviews];

    UIRectCorner rectCorner;
    BOOL roundCorners = YES;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:self];
    NSInteger numberOfRows = [self.tableView numberOfRowsInSection:indexPath.section];

    if (numberOfRows == 1) { // single cell
        rectCorner = UIRectCornerAllCorners;
    } else if (indexPath.row == numberOfRows - 1) { // bottom cell
        rectCorner = UIRectCornerBottomLeft | UIRectCornerBottomRight;
    } if (indexPath.row == 0) { // top cell
        rectCorner = UIRectCornerTopLeft | UIRectCornerTopRight;
    } else {
        roundCorners = NO;
    }

    if (roundCorners) {

        CGFloat cornerRadius = 10.0;
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                                       byRoundingCorners:rectCorner
                                                             cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.CGPath;
        self.layer.mask = maskLayer;
    }
}

You could avoid having tableView on a property by checking for self.superview.superview (iOS7, self.superview before), but I think it's a cleaner and less error-prone way.

kkodev
  • 2,557
  • 23
  • 23
0

Try setting the background color of the cells clearColor: Your table seems to have rounded corners, but your cells are overriding it. Another alternative solution might be to change the properties of the view behind the table. Try:

backgroundView.clipsToBounds = YES;

or

tableView.layer.cornerRadius = 5;
tableView.layer.masksToBounds = YES;

You may need to

#import <QuartzCore/QuartzCore.h>
Yunus Nedim Mehel
  • 12,089
  • 4
  • 50
  • 56
0

The trick is to set

view.layer.cornerRadius = 10;

Look at this sample project on this link! .

Legolas
  • 12,145
  • 12
  • 79
  • 132