Square instead of rounded corners in the style of UITableViewCell

I want to have square corners for my grouped table cells, and not rounded corners by default, and I don't just want to use an image to give an effect. Is it possible?

+4
source share
4 answers

Simply put, in tableView:cellForRowAtIndexPath: use

 cell.backgroundView = [[[UIView alloc] initWithFrame:cell.bounds] autorelease]; 
+13
source

You can configure the UITableViewCell backgroundView and selectedBackgroundView to a custom UIView that you create yourself. This should give you a square box.

+1
source

Accepted answers work well, but unfortunately dividing lines between cells are removed. If you have a table TableCellBackground.png with a size of 3x3 pixels, the top two rows of pixels in white and the lowest third row of gray (according to the color of the separator), you can do:

  // To square the corners, we replace the background view of the top and bottom cells. // In addition, the top cell needs a separator, which we get from TableCellBackground.png. UIImage *stretchableImage = [UIImage imageNamed:@"TableCellBackground.png"]; UIImage *cellImage = [stretchableImage resizableImageWithCapInsets:UIEdgeInsetsMake(1, 1, 1, 1)]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.bounds]; imageView.image = cellImage; cell.backgroundView = imageView; 
+1
source

First set the desired cell background screen, and then set the background view selected by the cell (within the same range as the cell background).

you can specify the cornerRadius property so that you can set the rounding of the corners as desired, I omitted this property in my case

Here is the code for both:

  UIView *bg = [[UIView alloc] initWithFrame:cell.bounds]; bg.backgroundColor = [UIColor colorWithRed:0.980 green:0.988 blue:0.984 alpha:1]; bg.layer.borderColor = [UIColor colorWithRed:0.827 green:0.827 blue:0.835 alpha:1].CGColor; bg.layer.borderWidth = kCellBorderWidth; // bg.layer.cornerRadius= kCellBorderRadius; cell.backgroundView = bg; // to make cell selection square and not round (which is by default) UIView *bg_selected = [[UIView alloc] initWithFrame:cell.bounds]; bg_selected.backgroundColor = [UIColor lightGrayColor]; bg_selected.layer.borderColor = [UIColor colorWithRed:0.827 green:0.827 blue:0.835 alpha:1].CGColor; bg_selected.layer.borderWidth = kCellBorderWidth; cell.selectedBackgroundView = bg_selected; 
0
source

Source: https://habr.com/ru/post/953023/


All Articles