丸みを帯びた角だけが必要な場合は、別の解決策があります(ネイティブの uitableview(cell) のグループ化されたスタイルに触発され、slowmo :) )。次のようになります。
セル サブクラス...
@interface MyCell : UITableViewCell
@property (nonatomic) top;
@property (nonatomic) bottom;
@end
@implementation MyCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = [UIColor clearColor]; // remove white default background
//example settings, UIImageViews with resizable images can be used too
self.backgroundView = [[UIView alloc] init];
self.selectedBackgroundView = [[UIView alloc] init];
self.backgroundView.backgroundColor = [UIColor greenColor];
self.selectedBackgroundView.backgroundColor = [UIColor blueColor];
self.backgroundView.layer.cornerRadius = self.selectedBackgroundView.layer.cornerRadius = 5; //
}
return self;
}
-(void)layoutSubviews{
[super layoutSubviews];
self.backgroundView.frame = self.selectedBackgroundView.frame = UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(self.top?0:-self.backgroundView.layer.cornerRadius, 0, self.bottom?0:-self.backgroundView.layer.cornerRadius, 0));
}
-(void)setTop:(BOOL)top{
_top = top;
[self setNeedsLayout];
}
-(void)setBottom:(BOOL)bottom{
_bottom = bottom;
[self setNeedsLayout];
}
@end
dataSource では、これを行うことができます:...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...cell initialization stuff...
[(MyCell *)cell setTop:(indexPath.row == 0)];
[(MyCell *)cell setBottom:(indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1)];
return cell;
}
長所:
- すべてのセルに 1 つの背景
- 使いやすい
- アニメート可能(画像ではなく位置を変更しています)
- プレーンスタイルにも使用できます(ただし、セクションに注意してください:))
短所:
- cell のサブクラスが必要です (rowHeight を変更した後でも背景の位置を調整する方法が見つかりませんでした)
- セクション全体の角が丸くなっているという問題は解決しません(セクションビューの角を手動で丸める必要があります)
これが誰かの役に立てば幸いですが、このコードはテストされていないことに注意してください! この回答を投稿する数分前にこのアイデアを実装しましたが、うまくいくようです:)