My iPhone app is currently displaying a grouped table with 2 sections. I tried to change the radius of the rounded corners on the first and last cells of each section (or on the same cell in the case of sections with just one row) using:
cell.layer.cornerRadius = 4;
or acting on the whole tableview:
self.tableView.layer.cornerRadius = 4;
but no luck...Of course I've imported QuartzCore before performing this operation. It looks like it's possible to customise the corners only when the table is displayed with plain style.
The ideal solution would be to customise top corners in the first one and bottom corners in the last.
Any suggestions?
The simplest way is:
Make a custom cells in a xib file, set their unique identifiers like:
@"beginCell", @"middleCell", @"endCell"
. You can make it according to this tutorial: http://www.bdunagan.com/2009/06/28/custom-uitableviewcell-from-a-xib-in-interface-builder/In method
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
you have to find ejther you are in the middle or on begin/end of the section and use the cell with proper identifier.What you can do is add a
UIView
on the background of a cell, make the cell background as clearcolor . Adjust theUIView
to have rounded corners.Here's what works in Swift 4.2:
And don't forget the
UITableView
background color to be transparent.If You need just rounded corners there's another solution (inspired by grouped style of native uitableview(cell), in slowmo :) ). It should be something like this:
Cell subclass...
In dataSource you can just do this:...
Pros:
Cons:
I hope this helps someone, but be aware that this code is not tested! I've implemented this idea few minutes before posting this answer and it just seems to work :)